【发布时间】:2016-02-28 17:41:03
【问题描述】:
我有一个 CollectionView,它允许用户触摸一个单元格,它会改变边框颜色。但是,我只想一次选择一个单元格。如何编辑此代码,以便使用边框颜色更新 indexpath 处的单元格并重置先前选择的单元格?
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.user["avatar"] = self.avatars[indexPath.row]
do {
try self.user.save()
} catch {
print(error)
}
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
cell.layer.borderWidth = 5.0
cell.layer.borderColor = UIColor.purpleColor().CGColor
谢谢!
更新
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
var previouslySelectedIndexPath: NSIndexPath?
if previouslySelectedIndexPath != nil {
let previousCell = collectionView.cellForItemAtIndexPath(previouslySelectedIndexPath!) as! AvatarViewCell
previousCell.layer.borderWidth = 0
previousCell.layer.borderColor = UIColor.whiteColor().CGColor
}
cell.layer.borderWidth = 5.0
cell.layer.borderColor = UIColor.purpleColor().CGColor
【问题讨论】:
标签: ios swift uicollectionview swift2