swift - 当我向下滚动表格 View 时,隐藏单元格的复选标记消失

标签 swift tableview cell checkmark

滚动前带有启用复选标记的单元格:

cell with enabled checkmark before scrolling

向上滚动后带有禁用复选标记的单元格:

cells with disabled checkmarks after i have scrolled up

嘿伙计们,我创建了一个表格 View 和一个添加按钮来添加新单元格。当我添加单元格并启用 UITableViewCell.AccessoryType.checkmark 时,当我滚动表格 View 并且单元格从 View 中消失时,复选标记会消失。 我该如何解决它?

   var waterValue = [String]()


    @IBAction func addButtonPressed(_ sender: Any) {




        let alert = UIAlertController(title: "add your amount of water for today", message: nil, preferredStyle: .alert)
        alert.addTextField(configurationHandler: {(waterAmountTodayTF) in waterAmountTodayTF.placeholder = "enter L for today"})
        alert.textFields?.first?.textAlignment = .center
        alert.textFields?.first?.becomeFirstResponder()
        alert.textFields?.first?.keyboardType = UIKeyboardType.decimalPad


        let action = UIAlertAction(title: "add", style: .default) {
            (_) in
            guard let waterAmountForToday = (alert.textFields?.first?.text) else { return }
            self.add("\(waterAmountForToday) L")
//            print(self.sliderValue)
        }
        alert.addAction(action)
        present(alert, animated: true)
    }



    func add(_ individualWaterAmount: String) {
        let index = 0
        waterValue.insert(individualWaterAmount, at: index)
        let indexPath = IndexPath(row: index, section: 0)
        tableView.insertRows(at: [indexPath], with: .left)
    }



    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70.0
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return waterValue.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let individualWaterValues = waterValue[indexPath.row]
        cell.textLabel?.text = individualWaterValues
        cell.textLabel?.textAlignment = .right
        cell.selectionStyle = .none
        return cell
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCell.AccessoryType.checkmark {
           tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none
        } else {
           tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.checkmark
        }
    }



    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        guard editingStyle == .delete else { return }
        waterValue.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }

最佳答案

创建新单元时,您尚未添加将附件类型设置为复选标记的逻辑。

您需要做的是,保存您选择的值,然后在创建新单元格时使用该值。

var selectedIndexes: [Int] = []; //global variable
...
...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let individualWaterValues = waterValue[indexPath.row]
        cell.textLabel?.text = individualWaterValues
        cell.textLabel?.textAlignment = .right
        cell.accessoryType = selectedIndexes.contains(indexPath.row) ? UITableViewCell.AccessoryType.checkmark : UITableViewCell.AccessoryType.none
        return cell
    }


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCell.AccessoryType.checkmark {
           tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none
           selectedIndexes.removeAll(where: { $0 == indexPath.row } )
        } else {
           tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.checkmark
           selectedIndexes.append(indexPath.row)
        }
    }

关于swift - 当我向下滚动表格 View 时,隐藏单元格的复选标记消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57834224/

相关文章:

swift - 如何从 Int8 读取最后两位?

swift - MVP - 在 iOS 中解散时从 View Controller 中销毁 Presenter 对象

objective-c - 如何从静态单元格表​​ View 中删除部分

JAVA:从mysql表中获取单元格

ios - 嵌入式 AVPlayer 不播放视频

swift - 为什么不能识别单元格的选择? swift

swift - 获取tableView内容的全高

ios - Swift - tableView 行高仅在滚动或切换展开/折叠后更新

Excel公式查找其他单元格使用的引用

excel - 在排除某些单元格/行的同时自动调整 Excel 中的列宽