集合视图单元格上的多个选择

时间:2017-03-25 13:46:23

标签: ios swift swift3 uicollectionview uicollectionviewcell

我正在尝试创建一个与iOS附带的照片应用程序具有类似功能的应用程序,我的功能允许您点按一个单元格,该单元格可以扩展图像以适应屏幕,然后点按图像以关闭它。我现在想要添加选择按钮,以便您可以选择可以下载的多个图像。我对此很陌生并环顾四周但却找不到能够完成这两件事的例子。任何帮助都会很棒。

更新

我目前的代码:

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return imagesURLArray.count
}

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

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

    cell.backgroundColor = .clear
    cell.imageView.image = UIImage(contentsOfFile: imagesURLArray[indexPath.row].path)
    cell.checkmarkView.isHidden = true

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {

    let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
    let availableWidth = view.frame.width - paddingSpace
    let widthPerItem = availableWidth / itemsPerRow

    return CGSize(width: widthPerItem, height: widthPerItem)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,insetForSectionAt section: Int) -> UIEdgeInsets {

    return sectionInsets
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

    return sectionInsets.left
}

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

    addZoomedImage(indexPath.row)
    addGestureToImage()
    addBackGroundView()

    view.addSubview(selectedImage)
}

我不知道从哪里开始,是否更容易添加长按手势来选择要下载的多个图像,或者在照片应用程序中选择按钮,这两个对于从哪里开始都有点无能为力。

2 个答案:

答案 0 :(得分:1)

好的 - 一种方法:

  • 设计一个提供“CheckMark”功能的自定义按钮(或找到已经制作的按钮)(点按以选中/取消选中)
  • 将该按钮添加到PhotoCell - 可能是左上角或右上角
  • 在CheckMark按钮上点击按钮时,选中/取消选中它并将消息发送回控制器以跟踪其状态
  • 如果单元格被点击但未点击复选标记按钮,请像现在一样处理didSelectItemAt indexPath:

这是一般的想法。对您而言,下一个好的地方可能是查看通过搜索uicollectionviewcell check mark

找到的一些示例

答案 1 :(得分:0)

//在swift中工作3.3希望有所帮助

 //my model struct

 struct Brands {
 var brandId: Int = 0
 var productId: Int = 0
 var name: String = ""
 var isChecked:Bool = false
 }

 var arrBrands = []//initialise arr of model

 func collectionView(_ collectionView: UICollectionView, 
 numberOfItemsInSection section: Int) -> Int {
 return arrBrands.count
 }

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

 let identifier: String = "brandCell"
 let cell: YourCustomCell? = 
 collectionView.dequeueReusableCell(withReuseIdentifier: identifier, 
 for: indexPath) as? YourCustomCell;

 let isChecked = arrBrands[indexPath.row].isChecked
 cell?.brandName.font = (isChecked) ?fontForTimesRoman(withStyle:"bold", andFontsize: 14):fontForTimesRoman(withStyle: "regular", 
   andFontsize: 14)
   cell?.brandName.text =  arrBrands[indexPath.row].name
  cell?.brandCheckButt.setImage((isChecked) ?UIImage(named:"filled_checkmark.png"):UIImage(named:"empty_checkmark.png"), for: .normal)

 return cell!
 }

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
 }

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  checkUncheckBrand(index: indexPath.row)
  }

  func collectionView(_ collectionView: UICollectionView, 
      didDeselectItemAt indexPath: IndexPath) {
       checkUncheckBrand(index: indexPath.row)
    }

   func  checkUncheckBrand(index:Int){
   arrBrands[index].isChecked = ! (arrBrands.isChecked)
   brandFilterCV.reloadData()
   }