停止重用自定义单元格Swift [英] Stop the reuse of custom cells Swift

查看:168
本文介绍了停止重用自定义单元格Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 uitableview ,其中包含一个从数组中获取数据的自定义单元格。
自定义单元格有一个 uilabel 和一个 uibutton (直到 uilabel 文本或加载文本的数组对象 - 为零。

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).

启动时一切正常。当我按 uibutton 时,正在追加数组,新单元格将被插入单元格下方。

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

但是当我滚动 - 突然之间 uibutton 出现在其他单元格中,其中没有隐含此条件 uilabel文本isEmpty

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

这是我的cellForRowAtIndexPath代码

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
}

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

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?

推荐答案

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

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.

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

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

更新:

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

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
}

这篇关于停止重用自定义单元格Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆