【发布时间】:2015-12-17 06:23:04
【问题描述】:
我有一个包含不同单元格的集合视图,我希望能够选择多个单元格。我可以这样做,但是当我单击一个已被选中的单元格时,相同的单元格会再次添加到数组中,从而导致重复。我想要发生的是,当它被点击时,它会将标签附加到数组中,当再次点击它时,它会将它从数组中删除。以下是我目前所拥有的。
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
arrayOfFriendsSelected.append(arrayOfFriendsNames[indexPath.item])
print(arrayOfFriendsSelected)
var cell = collection.cellForItemAtIndexPath(indexPath) as! ShareCell
cell.friendimage.layer.borderWidth = 3.0
cell.friendimage.layer.borderColor = UIColorFromRGB("4F26D8").CGColor
return true
}
func collectionView(collectionView: UICollectionView, shouldDeselectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
var cell = collection.cellForItemAtIndexPath(indexPath) as! ShareCell
arrayOfFriendsSelected.removeAtIndex(indexPath.item)
print(arrayOfFriendsSelected)
cell.friendimage.layer.borderWidth = 0.0
return true
}
【问题讨论】:
-
仅使用 didSelect 项目委托方法。在添加到数组之前,通过使用数组包含方法检查对象是否 der。如果 der 删除,则添加。
-
与其使用数组,不如使用集合。然后就可以快速查看好友是否已经在集合中了