解雇视图后如何保持收集视图单元格的状态?

问题描述 投票:0回答:3

所以我有一个collectionView,其中包含一系列趋势,用户可以单击以显示该照片。单击该单元格时,会出现一个对勾,让用户知道他们已经选择了该类别,然后将他们的photoId输入到数据库中选定的childValues中。

如果用户决定要从特定类别中删除他们的照片,他们可以选择编辑他们的照片。当我选择编辑配置文件时,未选择应选择的单元格(我在上传照片时选择的单元格)。

或者例如,我已经上传了一张照片,但是现在我要添加它,当我去编辑照片并点击一个类别时,会出现对勾标记,告诉我已选择此类别。当我按Save时,照片将按预期添加到数据库中所选的childValue上,但是当我再次单击edit profile并显示视图时。我10秒钟前选择的单元格现在未选中。

即使关闭视图控制器后如何保持选定或取消选定的状态?

   var selectedCategoryValues = [String]()
   var trend: Trend!
  var selectedIndex = -1

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

if collectionView == trendCollectionView {


let trendsCell = collectionView.dequeueReusableCell(withReuseIdentifier: 

"trendsCell", for: indexPath) as! TrendsCollectionCell

trendsCell.layer.cornerRadius = 9.0

trendsCell.layer.borderWidth = 0.1

trendsCell.layer.borderColor = UIColor.lightGray.cgColor


 if selectedIndex == indexPath.row {
            trendsCell.isSelected = true
             } else {

            trendsCell.isSelected = false

}


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

            if collectionView == trendCollectionView {

     guard selectedIndex != indexPath.row else{return}
     let indexpath = IndexPath(row:selectedIndex, section: 0)
     trendCollectionView.cellForItem(at: indexpath)?.isSelected = !trendCollectionView.cellForItem(at: indexpath)!.isSelected


        switch selectedSegmentIndex {

        case 0: self.trend = femaleTrends[indexPath.row]
        print("You selected  \(trend.childValue)")
        self.selectedCategoryValues.append(trend.childValue)




        case 1: self.trend = maleTrends[indexPath.row]
            print("You selected  \(trend.childValue)")
            self.selectedCategoryValues.append(trend.childValue)


             default: break

        }

集合视图单元格

class TrendsCollectionCell: UICollectionViewCell {


    override var isSelected: Bool{
        didSet{
            if isSelected {

              setSelectedUI()

            }
            else{

                setUnSelectedUI()

            }

        }
    }

    func setSelectedUI(){

    trendCheckmark.isHidden = false
    trendCheckmark.tintColor = .white

    }

    func setUnSelectedUI(){
        // reset to deafault, hide checkmark
        trendCheckmark.isHidden = true

    }

}
ios swift xcode uicollectionview uicollectionviewcell
3个回答
0
投票

您可以将所选索引存储为某些状态的一部分,然后根据需要存储/使用该状态。您可以通过具有一些属性selectedIndex来执行此操作,或者,如果具有更多属性,则可以在viewState中具有一些要更新的结构didSelectItemAt。然后,您可以在视图控制器之外使用一些状态管理器(推荐),或者在视图控制器之间传递状态(如果您有简单的应用程序)。


0
投票

维护单元格选定状态所需的三个步骤。这适用于table viewcollection view

1。保持最后选择的索引作为选定单元格。

2。在每个选择呼叫上更新cellindex

3。对于每种cellforrowindex方法,请检查此selected单元格是否。

进一步的解释是我先前的回答https://stackoverflow.com/questions/59293617/multiple-cells-selected-on-scrolling-reuse-cells-problem/


0
投票

我从不建议使用单元格选择的变量来检测单元格的选择,因为该变量不能完全描述状态,因为UICollectionView和UITableView的出队和回收机制如此,因此我建议每次都将所有状态都依赖于模型,所以你有2个选项

  1. 向每个单元格ModelObject添加isSelcted属性
  2. 向视图控制器添加保留所选索引的单个属性

导入基金会

 class NumberCountDataSource: NSObject , UICollectionViewDataSource {
        let selectionAction: ((Int)->())
        let numbersCount: Int
        var selectedIndex: Int?
        init(cardsCount: Int,selectionAction: @escaping ((Int)->())) {
            self.numbersCount = cardsCount
            self.selectionAction = selectionAction

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

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NumberCollectionViewCell.identiefier, for: indexPath) as! NumberCollectionViewCell
            cell.configure(number: indexPath.row + 1, isSelected: indexPath.row == selectedIndex)
            return cell
        }

    }

extension NumberCountDataSource: UICollectionViewDelegate , UICollectionViewDelegateFlowLayout {

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            selectedIndex = indexPath.row
            self.selectionAction(indexPath.row)
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return CGFloat.init(0.0)
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
            return CGFloat.init(0.0)
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize.init(width: 60, height: 50)
        }
    }

Class NumberCollectionViewCell:UICollectionViewCell {

@IBOutlet weak var numberLable: UILabel!
@IBOutlet weak var backGroundview: AnimatableView!
static let identiefier: String = "NumberCollectionViewCell"
static let nibName: String =  "NumberCollectionViewCell"
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
override var isSelected: Bool {
    didSet {

        setupSelection(iseSelected: isSelected)
}
}

private func setupSelection(iseSelected: Bool) {
    backGroundview.backgroundColor = isSelected ?  UIColor("#4d8ec5") : UIColor("#ffffff")
    backGroundview.borderColor = isSelected ?  UIColor.clear : UIColor("#707070")
    numberLable.textColor = isSelected ?  UIColor("#ffffff") : UIColor("#444e53")
}
func configure(number: Int,isSelected: Bool) {
    numberLable.text = "\(number)"
    setupSelection(iseSelected: isSelected)
}

}

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