【发布时间】:2015-11-25 09:06:13
【问题描述】:
我有一个uitableview,它带有一个从数组中获取数据的自定义单元格。
自定义单元格有一个 uilabel 和一个 uibutton(在 uilabel 文本或为文本加载的数组对象之前不可见 - 为 nil)。
启动时一切正常。当我按下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 上快速执行此操作,但也许在这个问题上有一些解决方法?
【问题讨论】:
-
这与它重用单元格无关,而与您没有正确编写它有关。
-
你能说出你想要达到的目标吗,似乎重用单元格并没有阻止你。
-
@Fogmeister 感谢您的评论和反馈。您将如何更改代码?
-
@DavidRobertson 在
TblCell中添加方法prepareForReuse。每次重用单元格时都会调用它。在此方法中,隐藏默认隐藏的任何内容。将标签设置为空白等... -
@Fogmeister 谢谢!听起来是个不错的解决方案)
标签: ios swift uitableview reusability