防止在 table 视图 Swift 中重复使用自定义单元格
Prevent reuse of custom cells in table view Swift
我有一个问题,我不确定如何解决。
我有两个自定义单元格笔尖 - 两者的数据都是从不同的数组中获取的。
结构如下
nib1-cell line1
nib1-cell line2
...
nib1-cell line n
nib2-cell line1
总是有一个 nib2-cell 在末尾 uibutton
。
一旦 uibutton
被按下 - nib1 数组被追加。
我想出了一种方法如何在 tableview
的底部插入值,但是当我向下或向上滚动时,带有 nib2 的单元格被重新使用并替换为 nib1-cell 数据。
如何防止这些单元格被重复使用或保存它们的状态?
谢谢。
更新:数据源和 cellForRowAtIndexPath 代码
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return someTagsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row < someTagsArray.count - 1){
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
cell.lblCarName.text = someTagsArray[indexPath.row]
return cell
} else if (indexPath.row == someTagsArray.count - 1){
var celle:vwAnswers = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! vwAnswers
celle.Answer1.setTitle(answersdict[answersdict.endIndex - 2], forState:UIControlState.Normal)
answertitle1 = "\(celle.Answer1.currentTitle!)"
celle.Answer2.setTitle(answersdict.last, forState:UIControlState.Normal)
answertitle2 = "\(celle.Answer2.currentTitle!)"
//println(answertitle2)
return celle
} else {
var cell2:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
return cell2
}
}
您必须确定 cellForRowAtIndexPath
中需要哪种类型的单元格并将正确的可重用单元格出队。也许像 if (indexPath.row + 1)%3 == 0
这样的东西然后出列一个应答单元格。
但是,您可能需要考虑使用 header 部分来代替。很难说没有看到你如何实现你的数据源。
我有一个问题,我不确定如何解决。
我有两个自定义单元格笔尖 - 两者的数据都是从不同的数组中获取的。
结构如下
nib1-cell line1
nib1-cell line2
...
nib1-cell line n
nib2-cell line1
总是有一个 nib2-cell 在末尾 uibutton
。
一旦 uibutton
被按下 - nib1 数组被追加。
我想出了一种方法如何在 tableview
的底部插入值,但是当我向下或向上滚动时,带有 nib2 的单元格被重新使用并替换为 nib1-cell 数据。
如何防止这些单元格被重复使用或保存它们的状态?
谢谢。
更新:数据源和 cellForRowAtIndexPath 代码
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return someTagsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row < someTagsArray.count - 1){
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
cell.lblCarName.text = someTagsArray[indexPath.row]
return cell
} else if (indexPath.row == someTagsArray.count - 1){
var celle:vwAnswers = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! vwAnswers
celle.Answer1.setTitle(answersdict[answersdict.endIndex - 2], forState:UIControlState.Normal)
answertitle1 = "\(celle.Answer1.currentTitle!)"
celle.Answer2.setTitle(answersdict.last, forState:UIControlState.Normal)
answertitle2 = "\(celle.Answer2.currentTitle!)"
//println(answertitle2)
return celle
} else {
var cell2:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
return cell2
}
}
您必须确定 cellForRowAtIndexPath
中需要哪种类型的单元格并将正确的可重用单元格出队。也许像 if (indexPath.row + 1)%3 == 0
这样的东西然后出列一个应答单元格。
但是,您可能需要考虑使用 header 部分来代替。很难说没有看到你如何实现你的数据源。