停止重复使用自定义单元格 Swift
Stop the reuse of custom cells 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
}
所以我的一般问题是如何停止重复使用这些自定义单元格?
据我所知,在 swift 中的 reusablecellswithidentifier
上没有直接的方法可以做到这一点,但也许在这个问题上有一些解决方法?
重复使用单元格时,它仍然具有之前使用时的旧值。
您必须通过重置显示隐藏控件的标志来准备重新使用它。
您可以在 tableView:cellForRowAtIndexPath:
或单元格的 prepareForReuse
方法中执行此操作。
更新:
这是您可以为 TblCell 添加的示例:
override func prepareForReuse()
{
super.prepareForReuse()
// Reset the cell for new row's data
self.act1.hidden = true
}
我有一个 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
}
所以我的一般问题是如何停止重复使用这些自定义单元格?
据我所知,在 swift 中的 reusablecellswithidentifier
上没有直接的方法可以做到这一点,但也许在这个问题上有一些解决方法?
重复使用单元格时,它仍然具有之前使用时的旧值。
您必须通过重置显示隐藏控件的标志来准备重新使用它。
您可以在 tableView:cellForRowAtIndexPath:
或单元格的 prepareForReuse
方法中执行此操作。
更新:
这是您可以为 TblCell 添加的示例:
override func prepareForReuse()
{
super.prepareForReuse()
// Reset the cell for new row's data
self.act1.hidden = true
}