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

时间:2018-06-21 17:44:26

标签: 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
}

2 个答案:

答案 0 :(得分:1)

要获取UICollectionViewCell相对于视图所在的位置(例如,相对于您的可见区域)的位置:

if let theAttributes = collectionView.layoutAttributesForItem(at: IndexPath(item: 1, section: 0)) {
     let cellFrameInSuperview = collectionView.convert(theAttributes.frame, to: collectionView.superview)
     print("X of Cell is: \(cellFrameInSuperview.origin.x)")
  }

答案 1 :(得分:1)

此类信息由集合视图的布局管理器保留。集合视图提供了两种方便的方法:

layoutAttributesForItem(at:)

和:

layoutAttributesForSupplementaryElement(ofKind:at:)

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

如果要在集合视图中显示特定项目的框架,请执行以下操作:

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

如果要在集合视图中显示节标题的框架,请执行以下操作:

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