【问题标题】:CollectionView multiple cell selectionCollectionView 多单元格选择
【发布时间】: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


【解决方案1】:

PhotoCollectionCellcell2_Class(或普通superclass)中只需重写此方法

- (void) setSelected:(BOOL)selected 
{ 
     if (selected) 
     {        
         self.layer.borderColor = UIColor.blueColor().CGColor
         self.layer.borderWidth = 3
     } 
     else 
     {
         self.layer.borderColor = UIColor.clearColor().CGColor
     }
}

那么您就不必在delegatedataSource 中处理实际的selection/highlighting

确保您的collectionView 具有allowsSelectionYES 的属性。

如果你想要multiple selection,那么还要将allowsMultipleSelection 设置为YES 并在你的delegate 中实现以下方法

- (BOOL) collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView.indexPathsForSelectedItems containsObject: indexPath])
    {
        [collectionView deselectItemAtIndexPath: indexPath animated: YES];
        return NO;            
    }
    return YES;
}

迅捷解决方案

collectionViewCell 的子类

override var selected: Bool {
    didSet {
        self.layer.borderWidth = 3.0
        self.layer.borderColor = selected ? UIColor.blueColor().CGColor : UIColor.clearColor().CGColor
    }
}

UICollectionViewDelegate

func collectionView(collectionView: UICollectionView, shouldSelectItemAt indexPath: NSIndexPath) -> Bool {
    if let selectedItems = collectionView.indexPathsForSelectedItems() {
        if selectedItems.contains(indexPath) {
            collectionView.deselectItemAtIndexPath(indexPath, animated: true)
            return false
        }
    }
    return true
}

【讨论】:

  • 非常感谢,但你能告诉我如何快速做到这一点吗,谢谢 agan
  • 我的 swift 不符合我的客观 c 标准,但是这个例子应该可以做到! =)
  • 非常感谢,我确实实现了你给我的解决方案,但是当我触摸一个单元格时没有任何反应
  • 感谢大家的帮助 :) ,, 遵循了这个解决方案,它再次奏效了 :) stackoverflow.com/questions/28596859/…
【解决方案2】:

基于 Aerows 解决方案 斯威夫特 4.2

collectionViewCell的子类

override var isSelected: Bool {
        didSet {
            self.layer.borderWidth = 3.0
            self.layer.borderColor = isSelected ? UIColor.blue.cgColor : UIColor.clear.cgColor
        }
    }

UICollectionViewDelegate

func collectionView(_ collectionView: UICollectionView, shouldDeselectItemAt indexPath: IndexPath) -> Bool {
    if let selectedItems = collectionView.indexPathsForSelectedItems {
        if selectedItems.contains(indexPath) {
            collectionView.deselectItem(at: indexPath, animated: true)
            return false
        }
    }
    return true
}

非常重要的是,在您的 viewDidLoad() 上不要忘记允许您的 collectionView 多选

collectionView.allowsMultipleSelection = true

Apple documentation - allowsMultipleSelection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多