【发布时间】:2016-09-18 14:05:25
【问题描述】:
我有一个包含两个自定义单元格的集合视图,一个用于网格,一个用于列表,我希望能够触摸单元格并选择它们,就好像要删除或共享它们一样,我现在只想能够选择和取消选择它们,我在下面发布我的代码结果是当我触摸一个单元格时,所有单元格都被选中!这是代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if isGridSelected {
let cell:cell2_Class = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! cell2_Class
cell.listImage.image = imageArray[indexPath.row]
if flag == true {
cell.layer.borderColor = UIColor.blueColor().CGColor
cell.layer.borderWidth = 3
cancelButton.hidden = false
} else {
cell.layer.borderColor = UIColor.clearColor().CGColor
cancelButton.hidden = true
}
return cell
} else {
let cell:PhotoCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PhotoCollectionCell
if flag == true {
cell.layer.borderColor = UIColor.blueColor().CGColor
cell.layer.borderWidth = 3
cancelButton.hidden = false
} else {
cell.layer.borderColor = UIColor.clearColor().CGColor
cancelButton.hidden = true
}
cell.imageView.image = imageArray[indexPath.row]
cell.NameLabel.text = namelabel[indexPath.row]
cell.ModifiedLbl.text = modfLabel[indexPath.row]
return cell
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
if cell!.selected == true {
flag = true
} else {
flag = false
}
self.collectionView.reloadData()
}
【问题讨论】:
-
为什么不改变DS然后重新加载collectionView?
-
谢谢,但我不太明白你的意思
-
这意味着,肯定有一些数组或某种数据结构。那么为什么不改变那里的值并简单地重新加载collectionview呢?
标签: ios arrays swift uicollectionview