Stop the reuse of custom cells Swift

我只是一个虾纸丫 提交于 2019-11-29 14:56:39

问题


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

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?


回答1:


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
}


来源:https://stackoverflow.com/questions/32309405/stop-the-reuse-of-custom-cells-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!