【问题标题】:Header section in the uicollectionview controller. out of rangeuicollectionview 控制器中的标题部分。超出范围
【发布时间】:2017-02-24 19:43:52
【问题描述】:

我正在使用viewForSupplementaryElementOfKind 函数应用来自uicollectionview 控制器的标题部分。但是,在viewDidAppear API 的异步解析之前,行索引被加载到viewForSupplementaryElementOfKind 函数中并超出范围。我该怎么办?

这是我的代码...

    override func viewDidAppear(_ animated: Bool) {
        callVideo3API()
    }

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:
            let row1 = self.list[0]
            let row2 = self.list[1]
            let row3 = self.list[2]

    let headerSection = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! HeaderSection


        headerSection.nameLabel01.text = row1.nickname
        headerSection.nameLabel02.text = row2.nickname
        headerSection.nameLabel03.text = row3.nickname


        return headerSection

    default:

        assert(false, "Unexpected element kind")
    }
}

【问题讨论】:

  • 它来是因为数据源可能是空的。因此,您必须在获取数据源后重新加载collectionView。并发布您的numberOfSectionsInCollectionViewnumberOfItemsInSection 方法。

标签: ios swift3 uicollectionview uicollectionreusableview


【解决方案1】:

您必须等到callVideo3API() 完成。成功完成callVideo3API() 后,您可以重新加载集合视图以获取输出。请按照以下步骤操作

  1. 调用方法callVideo3API()
  2. 通过 CollectionView 返回零使 CollectionView 为空 数据源 [func numberOfSections(in collectionView: UICollectionView) -> Int, func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int ]
  3. (可选)在执行 callVideo3API() 时,您可以在 CollectionView 的位置显示活动指示器
  4. 成功完成callVideo3API()后可以重新加载 CollectionView 具有相应的数据源值。这次它 将正常工作:-) (如果您放置活动指示器,请不要忘记在成功调用 api 后删除)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2016-07-05
    • 2013-11-01
    相关资源
    最近更新 更多