1

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

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
  • In cellForRowAt cellNumber[indexPath.row] < " " Why you are checking this? – Abhishek Jadhav Jan 04 '19 at 06:47
  • @AbhishekJadhav I was trying to check if that place in the array had any input, or if it was just an empty "". I now know that this is a faulty way of checking for input, but thanks for the heads up. – Karl Mogensen Jan 14 '19 at 15:43

1 Answers1

1

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.
Ajay saini
  • 2,352
  • 1
  • 11
  • 25
  • Okay so I dont really know what solution you want me to do. I am currently in the process of moving the @IBAction of UIButton to tableViewController. I have updated to question! – Karl Mogensen Jan 07 '19 at 15:49
  • Ok lets make it simple. Are you using cellNumber array to store the text of the cell's textfield ?? – Ajay saini Jan 07 '19 at 15:58
  • Okay appreciate it! yes I am, I append item to the array cellNumber at indexPath.row. I do this inside of the textViewDidEndEditing function. – Karl Mogensen Jan 07 '19 at 18:26
  • Then its really simple. Take a array. And append indexpath of the buttons cell when you mark it checked. And from cellforRowAt check that if the indexpath is in that array. If so then show that button as marked otherwise show it unmarked. What you are doing currently is you are keeping a Boolean value for each cell with change when cell reused – Ajay saini Jan 07 '19 at 18:29
  • And remove that indexpath from the array when you uncheck the button – Ajay saini Jan 07 '19 at 18:30
  • alright im with you so far, the problem now lies: how to I access the indexPath.row inside the configuration of the Button. Because the way I got the indexPath.row for the text array, I use the "TextView.frame.origin" and locating the cell in the window. – Karl Mogensen Jan 07 '19 at 18:42
  • Find answer by duncan c a tableview extension for doing this . https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped – Ajay saini Jan 07 '19 at 18:46
  • Or you can use delegate protocol for doing this. – Ajay saini Jan 07 '19 at 18:46
  • truly a beautiful solution which you've linked, and also has made me understand a different approach at configuring the cellForRowAt function. thank u a lot, will be posting the fixed code in the question today – Karl Mogensen Jan 08 '19 at 07:15
  • thankyou so much. and if solve your problem. i thing this answer deserve an thumbsup and selection. – Ajay saini Jan 08 '19 at 07:45
  • it certainly does! – Karl Mogensen Jan 09 '19 at 19:50