我有一个uitableview
,其中包含一个从数组中获取数据的自定义单元格。
自定义单元格包含uilabel
和uibutton
(在uilabel
文本或加载文本的数组对象之前不可见 - 为零)。
在发布时一切都很好。当我按下正在附加数组的uibutton
时,新单元格将插入单元格下方。
但是当我滚动时 - 突然uibutton
出现在其他单元格上,而这些条件uilabel text isEmpty
并不隐含。
这是我的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中执行此操作,但也许在这个问题上有一些解决方法?
答案 0 :(得分:17)
当一个单元格被重用时,它仍然具有之前使用的旧值。
您必须通过重置显示隐藏控件的标志来准备重用。
您可以在tableView:cellForRowAtIndexPath:
或单元格prepareForReuse
方法中执行此操作。
<强>更新强>
以下是您可以为TblCell添加的示例:
override func prepareForReuse()
{
super.prepareForReuse()
// Reset the cell for new row's data
self.act1.hidden = true
}