我为我的collectionview单元格写了一个upvote/downvote函数。Swift - CollectionView,如何查找IndexPath中的第一个和最后一个项目?
我将一个细胞的得分变量与它上面和下面的细胞进行比较。 收到upvote输入后,我有一个包含以下代码的函数。
if cell != CVcellArray.first && cell != CVcellArray.last {
print("NOW CHECKING IP")
let ip_greater = NSIndexPath(forItem: ip!.item - 1, inSection: ip!.section)
let ip_less = NSIndexPath(forItem: ip!.item + 1, inSection: ip!.section)
let cell_greater = self.collectionView?.cellForItemAtIndexPath(ip_greater) as! CollectionViewCell
let cell_less = self.collectionView?.cellForItemAtIndexPath(ip_less) as! CollectionViewCell
// recursive call to allow the cell to jump over repeat scores
if cell.score > cell_greater.score {
checkIPGreater(ip!, origIp: ip!)
}
else if cell.score < cell_less.score {
checkIPLesser(ip!, origIp: ip!)
}
else {
//do nothing
}
}
else {
print("No need to check ip")
}
checkIPLesser/Greater是查看重复分数的函数,以便单元格可以在upvoted时跳过多个空格。
当我upvote/downvote顶部/底部的地方单元格,我崩溃。这是我设想的,因为斯威夫特试图找到一个比最大的单元更大的单元,或者比最低的单元更低,并且它失败。所以我写了一行代码来检查被upvoted的单元格是在单元阵列的第一个单元格中,还是在单元阵列的末尾。
尽管如此,它仍然失败。我觉得我没有正确地找到顶部或底部的单元格。
我有一个部分,在该部分内有多个项目(这些项目是单元格)。有什么方法可以访问顶部或底部的商品吗?
好的确定,但我不确定如何执行“模型可以更新数据和重新排列项目”部分。我将不得不重新加载整个集合视图吗? – user3488148