簡體   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