1
votes

I have a tableview that contains 4 sections. In sections 2,3,and 4 I want to have a + button to add information to a "Saved" array. I have the logic setup for adding information, but I'm having issues with the tableview cells.

I don't want the + button to appear in section 0, since that's where we're adding the data. Here's my cellForRowAt method...

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

    // Configure the cell...

    if indexPath.section == 0 {
        cell.textLabel?.text = "Test"
        cell.addFavoritesButton.removeFromSuperview()
    } else if indexPath.section == 1 {
        cell.textLabel?.text = Items.sharedInstance.elementaryList[indexPath.row]
    } else if indexPath.section == 2 {
        cell.textLabel?.text = Items.sharedInstance.intermediateList[indexPath.row]
    } else if indexPath.section == 3 {
        cell.textLabel?.text = Items.sharedInstance.highschoolList[indexPath.row]
    }
    return cell

This works great at first! But if I scroll down, more and more cells will remove the button. It's not limiting it to section 0 because of reusable cells.

Can anyone think of a better way to remove this button for the first section only?

Screenshot of section 0

Screenshot of section 1

3
because you're reusing a cell that has done cell.addFavoritesButton.removeFromSuperview(). so when it's reused... * poof * bye bye forever sweet sweet addFavoritesButton, don't care which section you're in - staticVoidMan

3 Answers

2
votes

First run show the cells correctly because of all cells are new instances of the cell class (without reusing) , but after scroll shown cells may be reused with a possibility that this reused cell be the one in section zero which you removed the button from it , You can try to show/hide it

 if indexPath.section == 0 {
    cell.textLabel?.text = "Test"
    cell.addFavoritesButton.isHidden = true
} 
else
{
   cell.addFavoritesButton.isHidden = false
}
2
votes

You are forgetting that cells are reused. You need to deal, every time thru cellForRowAt, with the possibility that this cell already has the button from a previous use and should not have it in this use, or with the possibility that it lacks the button and needs it in this use.

For example, you cannot assume that just because the section is 1, the cell has the button, because it might have been used in section 0 earlier and lacks the button now. You need, in that case, to add it. But you are not doing that.

Thus, for every branch of your logic, you must be explicit about whether to add or remove the button. If you are really going to add and remove it, that can get complicated. You would need to keep a copy of the button somewhere, so you can add it. You'd make sure you don't add it twice to the same cell. You'd make sure you don't try to remove it if it is already removed.

As has been suggested in another answer, the simpler way to deal with this is not to add and remove at all, but to make visibility of the button dependent on whether this section is 0:

// do this in _every_ case
cell.addFavoritesButton.isHidden = (indexPath.section == 0)

That's a single line of code that does, much better, the thing you are trying to do.

1
votes

Once you remove the button from the cell by calling cell.addFavoritesButton.removeFromSuperview(), it would not be added back again for you when the cell is reused. You should keep the button on the cell, but hide it with

cell.addFavoritesButton.isHidden = indexPath.section == 0

or add a new feature that lets end-users remove items from section zero, and change the picture on the button from + to -:

enter image description here