【发布时间】:2016-11-20 16:53:35
【问题描述】:
我有一个带有集合视图的 FollowVC 和 FollowCell 设置。我可以使用以下代码将所有数据正确显示到我的 uIcollection 视图单元格中。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FollowCell", forIndexPath: indexPath) as? FollowCell {
let post = posts[indexPath.row]
cell.configureCell(post, img: img)
if cell.selected == true {
cell.checkImg.hidden = false
} else {
cell.checkImg.hidden = true
}
return cell
}
}
请注意,我还可以使用以下代码选择和取消选择多个图像
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if deletePressed == true {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
cell.checkImg.hidden = false
} else {
let post = posts[indexPath.row]
performSegueWithIdentifier(SEGUE_FOLLOW_TO_COMMENTVC, sender: post)
}
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FollowCell
cell.checkImg.hidden = true
}
在“选择”模式下,我可以对每个单元格进行选择,并且单元格上会显示一个复选标记。但是,我想要做的是有一个取消按钮来禁用所有选定的单元格并删除 checkImg。
我试过了
func clearSelection() {
print("ClearSelection posts.count = \(posts.count)")
for item in 0...posts.count - 1 {
let indexP = NSIndexPath(forItem: item, inSection: 0)
followCollectionView.deselectItemAtIndexPath(indexP, animated: true)
let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
cell.checkImg.hidden = true
}
}
程序在这里崩溃给我一个致命错误:Unexpectedly found nil while unwrapping an optional error at
let cell = followCollectionView.cellForItemAtIndexPath(indexP) as! FollowCell
我不知道为什么将单元格展开为包含 checkImg 实例的 FollowCell 时遇到问题。我之前已经在 didSelectItemAtIndexPath 的类似情况下使用过它,它似乎可以工作?
谢谢,
【问题讨论】:
标签: ios objective-c swift uicollectionview uicollectionviewcell