简体   繁体   中英

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

I have a tableViewController that has three functions, two of which are working successfully.

  1. Add cells by pressing button inside of toolbar = works

  2. Being able to insert text into UITextView inside of cell, without the text duplicating and moving around = works

  3. When button in cell is pressed, checkMarkImage appears, without getting duplicated when scrolling and moving around = does not work

The checkMarkImage does not stay put at the cell in which the button is pressed. It reappears when scrolling and sometimes disappears, despite my efforts of using a Boolean value, in order to track the cells of which have been checkmarked.

the configuration of the button inside of the my 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)
    }

}

The handling of cell inside 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
        }
    }

My expect the cell's checkMarkImage to stay at the cell in which the button is pressed, but the actual effect is that the checkMarkImage is re-occurring, when scrolling, and sometimes completely disappears

I am afraid that by doing this you not gonna achieve the desired outcome. you must persist your data when you using UITableView or UICollectionView because these reuse the Cell when scrolling. so when you scroll the UITableView you are getting duplicate image or sometime loosing it. what you can do is :

  1. Use an array of dictionary as dataSource to persist your data for the TableView.
  2. you can also create your own model and use as the dataSource of TableView .
  3. and if your data is large enough then you can go for CoreData / Sqllight .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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