Stop the reuse of custom cells Swift

David Robertson picture David Robertson · Aug 31, 2015 · Viewed 12.2k times · Source

I have an uitableview with a custom cell which gets data from the array. Custom cell has an uilabel and an uibutton (which is not visible until the uilabel text or the array object which loads for the text - is nil).

On launch everything is fine. When i press the uibutton the array is being appended, the new cells are being inserted below the cell.

But when i scroll - all of a sudden the uibutton appears on other cells where this conditional uilabel text isEmpty is not implied.

Here is how the whole process looks like enter image description here

Here is my code for cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:    NSIndexPath) -> UITableViewCell  {

    var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
    cell.lblCarName.text = someTagsArray[indexPath.row]

    if let text = cell.lblCarName.text where text.isEmpty {
        cell.act1.hidden = false
    } else {
        println("Failed")
    }

    cell.act1.setTitle(answersdict[answersdict.endIndex - 2], forState:UIControlState.Normal)
    cell.act2.setTitle(answersdict.last, forState:UIControlState.Normal)

    return cell
}

So my general question is how do i stop the reuse of those custom cells? As far as i'm aware there is no direct way of doing this on reusablecellswithidentifier in swift, but maybe there are some workarounds on that issue?

Answer

user4151918 picture user4151918 · Aug 31, 2015

When a cell is reused, it still has the old values from its previous use.

You have to prepare it for reuse by resetting that flag which showed your hidden control.

You can do this either in tableView:cellForRowAtIndexPath: or the cell's prepareForReuse method.

Update:

Here's an example you can add for TblCell:

override func prepareForReuse()
{
    super.prepareForReuse()
    // Reset the cell for new row's data
    self.act1.hidden = true
}