ios - 如何将单元格添加到 tableView 而不重新加载所有数据?

标签 ios swift uitableview

如何向 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/

相关文章:

ios - 生成 P12 文件 Xcode?

IOS 6 View Controller 不正确的宽度/边界 - 横向模式

ios - UIScrollView 在设备上的奇怪行为

swift - 使用 readLine() 跳过行 - Swift 2.2

iphone - 尝试强制重绘 UITableViewCell

iphone - iCloud 和 GEOResourceManifestServerRemoteProxy : Lost connection to geod warning

arrays - 如何在 Swift 中生成给定大小的 1 位和 0 位的所有排列

iOS 画外音 - 检测转子是否启用了垂直导航?

ios - Segue 不适用于静态 UITableView

iphone - 随着 UISlider 值的更改,单元格未更新值