reloadData不调用cellForItemAt

时间:2017-06-11 02:36:38

标签: ios swift xcode swift3

我跟着UICollectionView reloadData resigns first responder in section header在集合视图上实现了搜索栏,但是当调用collectionView?.reloadData()时,会显示下面的屏幕,我可以向下滚动查找已过滤的单元格。 我的数据源代码

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

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return section == 0 ? 0 : count // this got called after reloadData
}

override func collectionView(_ collectionView: UICollectionView,
                             cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(...)
    // this function did not get called after reloadData
}

3 个答案:

答案 0 :(得分:1)

发现问题...我将搜索栏高度设置为collectionView.contentSize,由于某种原因,我在reloadData后将其从50更改为2600

答案 1 :(得分:0)

确保在您的集合视图重新加载时,它有一些要显示的内容。如果数据源的值为0,则Count有一些值,那么您的cellForItem也不会被调用。

答案 2 :(得分:0)

如何重新加载单元格而不是所有数据?过滤数据源阵列后,您可以将countcollectionView.numberOfItemsInSection(1)进行比较,然后根据该值,您可以重新加载所需的单元格,如果需要更多,则删除其余单元格或添加。这样做的好处是你也可以获得很好的过滤动画。