繁体   English   中英

如何保存在单元格内按下按钮的动作,以便我可以添加图像而不会重复或消失

[英]how to save the action of a button press inside of a cell, so that I can add an image without it duplicating or disappearing

我有一个tableViewController ,它具有三个功能,其中两个运行成功。

  1. 通过按工具栏内的按钮添加单元格 = 有效

  2. 能够将文本插入到单元格内的 UITextView 中,而无需文本复制和移动 = 有效

  3. 按下单元格中的按钮时,会出现 checkMarkImage,滚动和移动时不会被复制 = 不起作用

checkMarkImage不会留在按下按钮的单元格中。 滚动时它会重新出现,有时会消失,尽管我努力使用布尔值来跟踪已被选中的单元格。

我的 customCellClass 内部按钮的配置:

 @IBAction func checkMarkButton(_ sender: UIButton) {

    if checkedOff{
        UIImageView.animate(withDuration: 0.3, delay: 0, options: [], animations: {

        self.checkMarkImage.alpha = 0.0
        self.notesTextView.isEditable = true
        self.notesTextView.textColor = .white
        self.checkedOff = false
        },completion: nil)
        }

    else{
        UIImageView.animate(withDuration: 0.3, delay: 0, options: [], animations: {

        self.checkMarkImage.alpha = 1.0
        self.notesTextView.isEditable = false
        self.notesTextView.textColor = .orange
        self.checkedOff = true
            }, completion: nil)
    }

}

cellForRowAt中单元格的处理:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifier = "TableViewNotesCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TableViewNotesCell

    cell.notesTextView.text = cellNumber[indexPath.row]




    if cellNumber[indexPath.row] < "  "{
        print("celltext is less than nothing")
        cell.notesTextView.textColor = .white
        cell.notesTextView.isEditable = true
        cell.checkMarkImage.alpha = 0.0
    }
    else{
        if cell.checkedOff{
            print("cell has been checked off")
            cell.notesTextView.textColor = .orange
            cell.notesTextView.isEditable = false
            cell.checkMarkImage.alpha = 1.0
        }
    }

我期望cell的checkMarkImage停留在按钮被按下的cell,但实际效果是checkMarkImage重新出现,滚动的时候,有时会完全消失

我担心这样做你不会达到预期的结果。 使用UITableViewUICollectionView时必须保留数据,因为它们在滚动时重用Cell 所以当你滚动 UITableView 时,你会得到重复的图像或有时会丢失它。 你可以做的是:

  1. 使用字典数组作为dataSource来保存 TableView 的数据。
  2. 您还可以创建自己的模型并用作TableView
  3. 如果您的数据足够大,那么您可以选择CoreData / Sqllight

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM