CollectionView多个单元格选择

问题描述 投票:4回答:2

我有一个集合视图,有两个自定义单元格,一个用于网格,一个用于列表,我希望能够触摸单元格并选择它们,如果要删除或共享它们,我现在想要的只能选择和deselct他们,生病我的代码下面的结果是当我触摸一个单元格所有单元格被选中!这是代码:

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()
}
ios arrays swift uicollectionview
2个回答
10
投票

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
     }
}

然后你不必处理你的selection/highlightingdelegate中的实际dataSource

确保你的collectionViewallowsSelection属性YES

如果你想要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;
}

Swift Solution

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
}

1
投票

基于Aerows解决方案Swift 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

© www.soinside.com 2019 - 2024. All rights reserved.