CollectionView多个单元格选择 [英] CollectionView multiple cell selection

查看:219
本文介绍了CollectionView多个单元格选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个自定义单元格的集合视图,一个用于网格,一个用于列表,我希望能够触摸单元格并选择它们,就像要删除或共享它们一样,我现在想要的全部是能够选择并删除它们,不适用于发布我的代码,结果是当我触摸一个单元格时,所有单元格都被选中!这是代码:

I have a collection view with two custom cells one is for grid and one is for list, i want to be able to touch cells and select them as as if want to delete or share them , all i want for now to be able to select and deselct them, ill post my code below the result is when i touch one cell all cells are selected! here is the code:

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

推荐答案

PhotoCollectionCellcell2_Class(或常见的superclass)中,只需覆盖此方法

In PhotoCollectionCell and cell2_Class (or in a common superclass) simply override this method

- (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.

Then you don't have to deal with the actual selection/highlighting in your delegate or dataSource.

请确保您的collectionView具有allowsSelectionYES的属性.

Make sure to have your collectionView has the property allowsSelection to YES.

如果需要multiple selection,还可以将allowsMultipleSelection设置为YES并在delegate

If you want multiple selection then also set allowsMultipleSelection to YES and implement the following method in your 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
}

这篇关于CollectionView多个单元格选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆