停止重用自定义单元格Swift

时间:2015-08-31 10:43:06

标签: ios swift uitableview reusability

我有一个uitableview,其中包含一个从数组中获取数据的自定义单元格。 自定义单元格包含uilabeluibutton(在uilabel文本或加载文本的数组对象之前不可见 - 为零)。

在发布时一切都很好。当我按下正在附加数组的uibutton时,新单元格将插入单元格下方。

但是当我滚动时 - 突然uibutton出现在其他单元格上,而这些条件uilabel text isEmpty并不隐含。

以下是整个过程的样子 enter image description here

这是我的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
}

所以我的一般问题是如何停止重复使用这些自定义单元格? 据我所知,在reusablecellswithidentifier中没有直接的方法可以在swift中执行此操作,但也许在这个问题上有一些解决方法?

1 个答案:

答案 0 :(得分:17)

当一个单元格被重用时,它仍然具有之前使用的旧值。

您必须通过重置显示隐藏控件的标志来准备重用。

您可以在tableView:cellForRowAtIndexPath:或单元格prepareForReuse方法中执行此操作。

<强>更新

以下是您可以为TblCell添加的示例:

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