Stop reusing custom Swift cells

I have a uitableview with a custom cell that gets data from an array. The user cell has uilabel and uibutton (which is not visible until the uilabel text or the array object that is loaded for the text is non-zero).

At startup, everything is fine. When I click uibutton , the array is added, new cells are inserted under the cell.

But when I scroll - suddenly uibutton appears in other cells where this conditional uilabel text isEmpty not implied.

Here is the whole process 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 to stop the reuse of these custom cells? As far as I know, there is no direct way to do this on the reusablecellswithidentifier in swift, but maybe there are some workarounds for this problem?

+5
source share
1 answer

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

You must prepare it for reuse by dropping this flag that showed your hidden control.

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

Update:

Here is an example you can add for TblCell:

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

All Articles