[英]dequeueReusableCellWithIdentifier returns different cell for the same indexpath
[英]UITableViewCell dequeuereusablecellwithidentifier returns the same cell
我正在创建一个UITableView,使用户能够添加可变数量的数据。 表格最初看起来像这样:
当用户单击“ +”按钮时,我想添加一个带有UITextField的新单元格以输入数据。 此新单元格是一个名为“ RecordValueCell”的自定义UITableViewCell。 如下所示:
//Custom UITableViewCell
class RecordValueCell : UITableViewCell {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var deleteButton: UIButton!
var onButtonTapped : ((_ sender : UIButton)->Void)?
@IBAction func deleteButtonTouched(_ sender: Any) {
guard let senderButton = sender as? UIButton else {
return
}
onButtonTapped?(senderButton)
}
}
但是,当我尝试使用tableView.dequeueReusableCell(withIdentifier: )
函数添加另一个单元格时,似乎返回了同一单元格。 这是我的UI外观:
我的新单元格应位于的部分顶部的空白区域。 这是添加单元格的代码:
func addNewValueCell() {
guard let reusableValueCell = self.tableView.dequeueReusableCell(withIdentifier: "valueCell") as? RecordValueCell else {
fatalError("failed to get reusable cell valueCell")
}
var cell = Cell() //some custom cell Object
//add the gray horizontal line you see in the pictures
reusableValueCell.textField.addBorder(toSide: .Bottom, withColor: UIColor.gray.cgColor, andThickness: 0.5)
reusableValueCell.onButtonTapped = { (sender) in
self.removeValue(sender: sender)
}
cell.cell = reusableValueCell
self.sections[self.sections.count - 1].cells.insert(cell, at: 0)
//When i put a break point at this spot, i find that reusableValueCell is the same object as the cell that is already being used.
tableView.reloadData()
reusableValueCell.prepareForReuse()
}
当我调试它时,我发现dequeueReusableCell(withIdentifier: )
:)多次返回完全相同的RecordValueCell。
这是我的cellForRowAt
:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = self.sections[indexPath.section].cells[indexPath.row].cell else {
fatalError("error getting cell")
}
return cell
}
numberOfRowsInSection
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sections[section].cells.count
}
首先,您需要将包含此表的View Controller类设置为表的UITableViewDataSource
tableView.dataSource = self // view controller that contains the tableView
创建一个字符串数组作为View Controller类的成员,其中包含每个单元格的数据:
var strings = [String]()
然后,您将需要为UITableViewDataSource协议实现以下方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return strings.count
}
您还应该像下面这样使cellForRowAt方法中的单元出队:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: yourIdentifier) as! YourCellClass
cell.textLabel = strings[indexPath.row]
return cell
}
然后,每当用户输入textField时,他们的输入将附加到此数组:
let input = textField.text strings.append(input) tableView.reloadData()
一旦重新加载数据,该单元格将自动添加到表中,因为行数由String数组的长度定义,并且标签在cellForRowAt方法中设置。
如果您做得很好,此功能非常容易实现。
首先,您必须创建两个TableCell。 第一个选项是使用加号按钮添加记录,第二个选项是使用文本字段输入值。 现在,始终返回tableView的最后一行中的第一个单元格(AddRecordTableCell),并根据输入的值(例如
返回totalValues.count + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.