Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 取消视图后如何维护集合视图单元格的状态?_Ios_Swift_Xcode_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 取消视图后如何维护集合视图单元格的状态?

Ios 取消视图后如何维护集合视图单元格的状态?,ios,swift,xcode,uicollectionview,uicollectionviewcell,Ios,Swift,Xcode,Uicollectionview,Uicollectionviewcell,所以我有一个collectionView,它保存了一系列的趋势,用户可以点击它来展示这张照片。单击单元格时,会出现一个复选标记,让用户知道他们已选择该类别,然后将其photoId输入数据库中选定的childValues 如果用户决定从某个类别中删除照片,则可以选择编辑照片。当我选择编辑配置文件时,应该选择的单元格(上传照片时选择的单元格)将取消选择 或者说,例如,我已经上传了一张照片,但现在我想展示它,当我去编辑照片并点击一个类别时,会出现复选标记,告诉我选择了这个类别。当我按save时,照片会

所以我有一个collectionView,它保存了一系列的趋势,用户可以点击它来展示这张照片。单击单元格时,会出现一个复选标记,让用户知道他们已选择该类别,然后将其photoId输入数据库中选定的childValues

如果用户决定从某个类别中删除照片,则可以选择编辑照片。当我选择编辑配置文件时,应该选择的单元格(上传照片时选择的单元格)将取消选择

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

即使在解除视图控制器后,如何保持选定或取消选定状态

var selectedCategoryValue=[String]()
趋势:趋势!
变量selectedIndex=-1
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
如果collectionView==trendCollectionView{
让trendsCell=collectionView.DequeueReuseAbleCell(withReuseIdentifier:
“trendsCell”,用于:indexath)as!TrendsCollectionCell
trendsCell.layer.cornerRadius=9.0
trendsCell.layer.borderWidth=0.1
trendsCell.layer.borderColor=UIColor.lightGray.cgColor
如果选择了dex==indexPath.row{
trendsCell.isSelected=true
}否则{
trendsCell.isSelected=false
}
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
如果collectionView==trendCollectionView{
guard selectedIndex!=indexPath.row else{return}
让indexpath=indexpath(行:selectedIndex,节:0)
trendCollectionView.cellForItem(位于:indexpath)?.isSelected=!trendCollectionView.cellForItem(位于:indexpath)!.isSelected
开关选择分段索引{
案例0:self.trend=femaleTrends[indexath.row]
打印(“您选择\(trend.childValue)”)
self.selectedCategoryValue.append(trend.childValue)
案例1:self.trend=maleTrends[indexath.row]
打印(“您选择\(trend.childValue)”)
self.selectedCategoryValue.append(trend.childValue)
默认值:中断
}
集合视图单元

class TrendsCollectionCell:UICollectionViewCell{
选择覆盖变量:Bool{
迪塞特{
如果当选{
setSelectedUI()
}
否则{
setUnSelectedUI()
}
}
}
func setSelectedUI(){
trendCheckmark.isHidden=false
trendCheckmark.tintColor=.white
}
func setUnSelectedUI(){
//重置为DEAFULT,隐藏复选标记
trendCheckmark.isHidden=true
}
}

您可以将所选索引存储为某个状态的一部分,然后根据需要存储/使用该状态。您可以通过选择一些属性
索引来实现这一点,或者,如果您有更多属性,您可以在
didSelectItemAt
中更新一些结构
视图状态视图控制器的外侧(推荐)或在视图控制器之间传递状态(如果您有一个简单的应用程序)。

维护选定单元格状态所需的三个步骤。这适用于
表视图
集合视图

1。为选定单元格保留上次选定的索引

2.在每个did select呼叫上更新
cellindex

3.对于每个
cellforrowindex
方法,检查此
单元格是否选中

我先前的回答有进一步的解释

我从不建议使用cell selected变量来检测单元格选择,因为该变量不能完全描述状态,因为UICollectionView和UITableView都有排队和循环机制,所以我建议每次都将所有状态依赖于您的模型, 所以你有两个选择

  • 向每个单元格模型对象添加IsElcted属性
  • 向视图控制器添加保留选定索引的单个属性
  • 进口基金会

     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)
            }
        }
    
    类号CollectionViewCell: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)
    }
    

    }

    在Indexath Too处显示项目的单元格,你介意给我一个如何保留选定索引的示例吗?这就是我在示例中遇到的问题,我只是想显示数字卡片的集合。我很难理解这一点。因此,我是否要将选定字符串的数组和索引的编号保存为well?从未使用
    didSelect
    保存状态,因此如果我的问题是众所周知的,我很抱歉。我已经可以显示我想要的类别集合,当我选择/取消选择单元格时,复选标记会按预期隐藏和取消隐藏。上传照片时,照片也会以我选择的正确类别发布。Howev呃,当我去编辑那张照片以从我选择的类别中删除照片时,复选标记
    。ishiden=true
    应该在何时保存并保留复选标记。虽然每个答案都几乎相同,但你还是先回答了。非常感谢,这终于起作用了。