ios - UITableView - 在滚动时它会丢失与选定单元格关联的属性。如何保存这些属性? - swift

标签 ios swift uitableview

我有一个带有自定义弹出窗口的应用程序,其中包含一个表格 View (见下文)。当我点击一个复选框(UIButton)时,我换掉了背景图像并在选中和未选中之间切换。当我有一个选中的框时,当我向上或向下滚动时,选中的框默认恢复为未选中。请有人建议如何在滚动时保持每个单元格的选中状态?

enter image description here

 class CustomHashTagPopup: UIViewController, UITableViewDelegate, UITableViewDataSource{


// CREATE TABLEVIEW CELLS
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = self.tableViewPicker.dequeueReusableCell(withIdentifier: "pickerCell") as! PreviewTableViewCustomPickerCell

    if(indexPath == [0,0]){
        let image = UIImage.init(named: "add")
        cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
        cell.pickerLabel.text = "Create New HashTag"
        cell.isCreateTagCell = true

    }else{
        let image = UIImage.init(named: "uncheck")
        cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
        cell.pickerLabel.text = arrayHashTags[indexPath.row]
        cell.isCreateTagCell = false
    }
    return cell

}


 }


 class PreviewTableViewCustomPickerCell: UITableViewCell {

 var isSelect: Bool = false
var isCreateTagCell: Bool = false // TO DISTINGUISH 'CEATE NEW HASHTAG OPTION'



/*** OUTLETS ***/

@IBOutlet weak var checkBoxOutlet: UIButton!
@IBOutlet weak var pickerLabel: UILabel!


 // TAP ON CHECK BOX
@IBAction func checkBoxBtn(_ sender: Any) {


    if(isSelect == false && isCreateTagCell == false){
        isSelect = true
        // SHOW GREEN SELECTED CHECK BOX
        let image = UIImage.init(named: "tick")
        self.checkBoxOutlet.setBackgroundImage(image!, for: .normal)

    }else if(isSelect == true && isCreateTagCell == false){
        isSelect = false
        // SHOW UNCHECKED BOX
        let image = UIImage.init(named: "uncheck")
        self.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
    }
}

最佳答案

你错过的是 cell-reusing ,你必须将每个 Action 存储在模型数组中并检查当前值并在 cellForRowAt 中设置适当的设置,你可以设置 Action 的委托(delegate)到 cellForRowAt 内按钮的 vc,以便在用户单击特定 indexPath 处的按钮时轻松访问模型数组以对其进行操作,因此请考虑像这样声明数组

let arr = [false,true,false,false,false] first index is dummy as it will be skipped in Create New HashTag

//

@objc func btnClicked(_ sender:UIButton) {
   arr[sender.tag] = !arr[sender.tag]
    // reload indexPath of row = sender.tag , section = 0
}

//

cellForRowAt 内部

if (indexPath == [0,0]) {
    let image = UIImage(named: "add")
    cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
    cell.pickerLabel.text = "Create New HashTag"
    cell.isCreateTagCell = true

}else{
    let image = UIImage(named: arr[indexPath.row] ? "tick" : "uncheck") // edited here 
    cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
    cell.pickerLabel.text = arrayHashTags[indexPath.row]
    cell.isCreateTagCell = false
}

cell.button.addTarget(self, action: #selector(btnClicked(_:)), for: .touchUpInside)
cell.button.tag = indexPath.row

关于ios - UITableView - 在滚动时它会丢失与选定单元格关联的属性。如何保存这些属性? - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066663/

相关文章:

ios - 具有覆盖占位符的自定义 UITextField 无法按预期工作

iphone - iCloud 的有效文件类型?

Swift 结构项值不随函数改变

swift - UIButton 的 textLabel 未被剪切

ios - 在 UISearchController 和 FetchRequest 之间同步复选标记

ios - 加载 ICarousel 的 MPMovieControl 无法正常工作

swift - 未遵守程序约束

ios - 无法使用协变类型 'refreshControl' 覆盖类型 'UIRefreshControl?' 的可变属性 'UIRefreshControl'

ios - 显示 UItableViewcell 选择

ios - 从一个 View Controller 传递字符串并将其显示在 ios 的 UITextView 中