【发布时间】:2016-07-29 07:45:40
【问题描述】:
我的目标是在用户单击单元格时制作边框。
以下代码有效:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Cell \(indexPath.row) selected")
}
然后我添加了这段代码以使边框着色但没有效果。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell : MKStandItemAdd = collectionView.dequeueReusableCellWithReuseIdentifier("ItemCell", forIndexPath: indexPath) as! MKStandItemAdd
cell.layer.borderWidth = 7
cell.layer.borderColor = UIColor.blueColor().CGColor
print("Cell \(indexPath.row) selected")
}
上面这段代码仍然可以打印,但没有改变颜色。
所以我补充说:
self.collectionView.reloadData()
但现在什么都没有发生。它不再打印,也不会重新加载集合。
【问题讨论】:
-
不要调用 reloadData()。重新加载特定单元格。
-
当您单击单元格时.. 存储该索引,然后根据索引在
cellForItemAtIndexPath方法中创建边框 -
尝试添加
cell.layer.masksToBounds = YES;
标签: ios swift xcode uicollectionview