Dav*_*son 5 reusability uitableview ios swift
我有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
}
Run Code Online (Sandbox Code Playgroud)
所以我的一般问题是如何停止重用那些自定义单元格?据我所知,没有直接的方法可以reusablecellswithidentifier快速执行此操作,但也许在这个问题上有一些解决方法?
小智 17
当一个单元被重用时,它仍然具有之前使用的旧值.
您必须通过重置显示隐藏控件的标志来准备重用.
您可以tableView:cellForRowAtIndexPath:在单元格的prepareForReuse方法中执行此操作.
更新:
以下是您可以为TblCell添加的示例:
override func prepareForReuse()
{
super.prepareForReuse()
// Reset the cell for new row's data
self.act1.hidden = true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10120 次 |
| 最近记录: |