4

在表格视图中,我们可以轻松地在单元格上添加复选标记。

但是在集合视图中,当我们选择一个单元格(图像)时,我们如何打勾?

我只是在单元格和图像视图中拍摄了一个图像视图并放置了一个刻度线图像。我的代码如下。

但它不起作用。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{

   //  handle tap events
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! customCollectionViewCell


    if(cell.checkMarkImage.hidden == true)
    {
        print("Hidden")
       cell.checkMarkImage.hidden = false

    }
    else
    {
         cell.checkMarkImage.hidden = true
        print("No Hidden")
    }
}
4

6 回答 6

4

//委托方法 cellForItemAtIndexPath

func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell
{
    //Get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
                                                            "pickSomecell",
                   forIndexPath: indexPath) as! pickSomeGridViewController

    //Show Images in grid view
    cell.cellImage.image = self.arrAllOriginalImages[indexPath.row]
                                                            as? UIImage        

    //Check Mark toggle.
    cell.toggleSelected()      

    //return cell.
    return cell
}

并在 pickSomeGridViewController 中显示选中或未选中的选中标记图像。

class pickSomeGridViewController: UICollectionViewCell{
//Outlet of cell image.
@IBOutlet var cellImage: UIImageView!

//Outlet of checkMark image.
@IBOutlet var cellCheckMarkImage: UIImageView!

//Function  for select and deselect checkmark.
  func toggleSelected ()
  {
    //If image is selected.
    if (selected)
    {
            //Show check mark image.
            self.cellCheckMarkImage.hidden = false           
    }   

    else
    {
             //Hide check mark image.
            self.cellCheckMarkImage.hidden = true            
    }
  }   
 }
于 2016-07-20T07:58:36.397 回答
1

var arrData = NSMutableArray() // 1.使用模态类对象创建一个 ModalClass.swift 和 NSArray

 class CustomModal: NSObject {
        //Declare bool variable for select  and deselect login
        var is_selected = Bool()
        //you can declare other variable also
        var id = Int32()

}




// 2. custom array with modal objects

    override func viewDidLoad() {
        super.viewDidLoad()
        let arrTemp = NSArray()
        arrTemp = [1,2,3,4,5,6,7,8,9,10]
        for i in 0 ..< arrTemp.count{
            let eventModal = CustomModal()
            eventModal.is_selected = false
            eventModal.id = arrTemp[i] 
            arrData.add(eventModal)
        }
        tblView.reloadData()

    }

// 2. 使用集合视图委托方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let modal = arrData[indexPath.row] as! CustomModal()
    modal.is_selected = true
    self.arrData.replaceObject(at: indexPath.row, with: modal)

    tblView.reloadData()
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let modal = arrData[indexPath.row] as! CustomModal()
    modal.is_selected = false
    self.arrData.replaceObject(at: indexPath.row, with: modal)

    tblView.reloadData()
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! YourCellClass
    let modal = arrData[indexPath.row] as! CustomModal
    if modal.is_selected == true{
        cell.imgView.image = UIImage(named:"selected_image")
    }else{
        cell.imgView.image = UIImage(named:"deselected_image")
    }
}
于 2016-12-06T10:14:36.073 回答
1

我看到这段代码有两个主要问题:

  1. 您使用从集合视图缓存dequeueReusableCellWithReuseIdentifier中获取不同单元格的方法,而不是屏幕上的那个。改为使用cellForItemAtIndexPath集合视图的方法。
  2. 您尝试将单元格的状态(选择/未选择)保存在单元格本身中。UITableView使用/时这是一个常见的错误UICollectionView,这种方法不起作用。相反,将状态保存在其他位置(例如在字典中)并在每次集合视图调用您的数据源方法时恢复它。cellForItemAtIndexPath
于 2016-04-19T10:41:53.907 回答
0
var selectedCellIndex:Int?

如果要在 reloadData() 之后显示选定的项目,请使用变量:这是先前选择的 CellItem。{受上述答案的启发}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColorCollectionCell", for: indexPath) as! ColorCollectionCell

        cell.isSelected = false

        if selectedCellIndex == indexPath.item {
            cell.checkMarkImgView.image = UIImage(named: "icn_checkMark")
        }else {
            cell.toggleSelected()
        }

        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cell = collectionView.cellForItem(at: indexPath) as! ColorCollectionCell
        cell.isSelected = true
        selectedCellIndex = indexPath.item
        cell.toggleSelected()
}

在 CollectionViewCell 中你可以使用这个方法

class ColorCollectionCell: UICollectionViewCell {
    @IBOutlet weak var cellimgView: UIImageView!
    @IBOutlet weak var checkMarkImgView: UIImageView!

     func toggleSelected() {

        if (isSelected) {
            self.checkMarkImgView.image = UIImage(named: "icn_checkMark")
        }else{
            self.checkMarkImgView.image = UIImage(named: "")
// here you can use uncheck img here i am not using any image for not selected.
        }
    }   
}
于 2020-04-04T18:17:03.517 回答
0

斯威夫特 4

在视图控制器中

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCollectionViewCellID", for: indexPath as IndexPath) as! YourCollectionViewCell
    cell.someImageView.image = imgArr[indexPath.item]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    print("You selected cell #\(indexPath.item)!")
    let cell = collectionView.cellForItem(at: indexPath) as? YourCollectionViewCell
    cell?.isSelected = true
    cell?.toggleSelected()    
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    let cell = collectionView.cellForItem(at: indexPath) as? YourCollectionViewCell
    cell?.isSelected = false
    cell?.toggleSelected()
}

在 YourCollectionViewCell 中

class YourCollectionViewCell: UICollectionViewCell {


    @IBOutlet weak var someImageView: UIImageView!
    @IBOutlet weak var checkImageView: UIImageView!

    //Function  for select and deselect checkmark.
    public func toggleSelected() {

        if (isSelected == false) {

            //Hide check mark image.
            self.checkImageView.image = UIImage(named: "unCheckImage")
            isSelected = true
        }else{

            //Show check mark image.
            self.checkImageView.image = UIImage(named: "CheckImage")
            isSelected = false
        }
    }    
}

希望享受!!

于 2019-02-06T17:47:58.303 回答
0

@Kishor,paintcode 是您可以通过它执行此操作的第三方工具。我也提供了链接。由于默认情况下您没有此功能,因此您应该通过自定义行为来实现这一点。谢谢。

于 2016-03-24T05:43:17.513 回答