如何向 tableView 添加单元格(自定义)?我尝试过以下方法:
@IBAction func AddCommentButton(_ sender: Any) {
addComment() {
//1)
self.tableView.beginUpdates()
tableView.reloadRows(at: [arrayOfComments.count].indices, with: .automatic)
self.tableView.endUpdates()
//2)
// let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell", for: tableView.numberOfRows(inSection: 0)) as! commentTableViewCell
// addTemporaryComment(cell: cell)
}
}
1)产量:
Cannot convert value of type 'Range.Index>' (aka 'Range') to expected argument type '[IndexPath]'
2)使用此方法也会产生许多错误:全部都是;
Use of unresolved identifier 'indexPath'; did you mean 'IndexPath'?
func addTemporaryComment(cell: commentTableViewCell) {
do {
let url = URL(string: (arrayOfComments[indexPath.row].user.profileImageUrlString)!)
let imageData = try Data(contentsOf: url!)
let image = UIImage(data: imageData)
cell.dateOfComment.text = arrayOfComments[indexPath.row].date
cell.commentText.text = arrayOfComments[indexPath.row].commentText
cell.profImage.image = image
cell.username.text = arrayOfComments[indexPath.row].user.username
} catch {
print(error, ": Failed in do block converting image")
}
}
}
我什至不知道两者是否有效。
如果这些是正确的做法,我该如何解决这些问题?如果不是,我该如何使其发挥作用?
最佳答案
第一步应该是将新数据附加到 TableView 用于填充数据的数组
YourTablearray.append([labeltext])
接下来更新表格数据以在表格末尾插入单元格
tableName.beginUpdates()
tableName.insertRowsAtIndexPaths([
NSIndexPath(forRow: YourTablearray.count-1, inSection: 0)], withRowAnimation: .Automatic)
tableName.endUpdates()
关于ios - 如何将单元格添加到 tableView 而不重新加载所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55788316/