ios - Swift iOS - 如何找到 CollectionView 第二部分的 CGPoint x/y 值

标签 ios swift uicollectionview cgrect cgpoint

我有一个包含 2 个部分的 collectionView。第一部分的框架将取决于其中的单元格数量。由于第一部分中的单元格总数 (firstSectionData.count) 会有所不同,我需要找到第二部分的 CGPoint。我特别需要找出第二部分开始位置的 x/y 值。

框架也足够了,因为我可以从那里提取它们。我怎样才能找到它?

var cv: UICollectionView!
let firstSectionCell = "FirstSectionCell"
let secondSectionCell = "SecondSectionCell"
let secondSectionHeaderView = "SecondSectionHeaderView"
let firstSectionData = [String]()
let secondSectionData = [String]()
let cellHeight: CGFloat = 50
let headerHeight: CGFloat = 60

override func viewDidLoad() {
    super.viewDidLoad()

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    layout.scrollDirection = .horizontal

    cv = UICollectionView(frame: view.frame, collectionViewLayout: layout)
    cv.delegate = self ; cv.dataSource = self
    cv.register(FirstSectionCell.self, forCellWithReuseIdentifier: firstSectionCell)
    cv.register(SecondSectionCell.self, forCellWithReuseIdentifier: secondSectionCell)
    cv.register(SecondSectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: secondSectionHeaderView)
    view.addSubview(cv)
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0{
        return firstSectionData.count // varies
    }
    return secondSectionData.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.section == 0{
        return firstSectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.firstSectionCell, for: indexPath) as! FirstSectionCell
    }

    return secondSectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.secondSectionCell, for: indexPath) as! SecondSectionCell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: self.cellHeight) // height is 50
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let headerView: UICollectionReusableView?
    if kind == UICollectionElementKindSectionHeader{
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.secondSectionHeaderView, for: indexPath) as! SecondSectionHeaderView
        headerView = header
    }
    return headerView!
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    if section == 0{
        return CGSize.zero // the first section doesn't have a header
    }

    return CGSize(width: view.frame.width, height: self.headerHeight) // header height is 60 for the second section
}

最佳答案

此类信息由 Collection View 的布局管理器保存。 Collection View 提供了两种方便的方法:

layoutAttributesForItem(at:)

和:

layoutAttributesForSupplementaryElement(ofKind:at:)

这些返回值 UICollectionViewLayoutAttributes?。这又具有 frame 等属性。

如果您想要 Collection View 中特定项目的框架,您可以这样做:

let frame = collectionView.layoutAttributesForItem(at: someIndexPath)?.frame

如果你想在 Collection View 中使用节标题的框架,你可以这样做:

let frame = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: someSectionIndexPath)

关于ios - Swift iOS - 如何找到 CollectionView 第二部分的 CGPoint x/y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50974686/

相关文章:

ios - 为什么 Swift 中 CFStringEncodings 没有 UTF8?

iphone - TTPhotoViewController:停用图库自动缩放

swift - 物理形状在接触时与对象的形状不匹配

ios - CollectionViewCell 不持久

android - 移动应用程序与服务器持续同步

ios - 自动续订订阅是否仅限于报亭?

ios - 如何隐藏或删除ui组件并移动其他组件以占据隐藏或删除的空间?

swift - 如何在 ARKit 中使用垂直检测?

ios - 如何在 UICollectionViewDataSource 和 UICollectionViewDelegateFlowLayout 方法中调用正确的数据源?

ios - UICollectionView 自定义行分隔符