如何使用viewForSupplementaryElementOfKind - swift显示UICollectionView的特定单元格

问题描述 投票:1回答:1

我是一个新的iOS编程。现在我正在开发一个类似于appstore的应用程序。但是我有一些问题,当用户打开应用程序时,我希望数据单元格显示第二个单元格。例如,我有三个单元格然后当用户打开应用程序时,第二个单元格应自动滚动到该位置。

这是我如何创建标题部分:(以编程方式)

/// banner section
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerCellId, for: indexPath) as! HeaderBanner

    header.sectionCategory = dashbordCell?[indexPath.item]

    return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    let size = CGSize(width: view.frame.width, height: 370)
    return size
}

这是我创建的HeaderBanner

let dataBannerCollectionView: UICollectionView = {

    var scrollInterval: Int = 3
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .horizontal
    let collectionView = UICollectionView(frame: .zero,collectionViewLayout: flowLayout)
    collectionView.backgroundColor = UIColor.clear
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    return collectionView

}()

func setupViewsCell(){
    dataBannerCollectionView.showsHorizontalScrollIndicator = false


    dataBannerCollectionView.dataSource = self
    dataBannerCollectionView.delegate = self

    dataBannerCollectionView.register(HeaderBannerDataCell.self, forCellWithReuseIdentifier: headerCellId)

    addSubview(dataBannerCollectionView)
    setupConstraintDataBannerCollectionView()



}

这是HeaderBanner文件的行为。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if let count = sectionCategory?.dataBanner?.count {

        return count
    }
    return 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: headerCellId, for: indexPath) as! HeaderBannerDataCell
    cell.dataBannerCell = sectionCategory?.dataBanner![indexPath.item]

    return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    print(frame.width)
    let size = CGSize(width: frame.width - 100 , height: 350)
    return size
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("Banner selected")
}

如您所见,标题部分显示图像的第一个单元格。当用户打开应用程序时,我的应用程序就像这样。

这是我的示例应用程序的输出:

这是我希望显示所有细胞的中间细胞的期望。

ios swift uiscrollview uicollectionview
1个回答
1
投票

您可以使用UICollectionView的scrollToItem函数:

如果您有三个单元格,例如:

        dataBannerCollectionView.scrollToItem(at: IndexPath(item: 2, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)

在setupViewsCell()函数中添加该代码

© www.soinside.com 2019 - 2024. All rights reserved.