簡體   English   中英

如何將單元格添加到tableView中以重新加載所有數據?

[英]How to add a cell to a tableView withought reloading all data?

如何將單元格(自定義)添加到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)收益:

無法將類型“ Range.Index>”(也稱為“范圍”)的值轉換為預期的參數類型“ [IndexPath]”

2)使用這種方法也會產生很多錯誤。

使用未解決的標識符“ indexPath”; 您是說'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")
    }
}

}

我什至不知道任何一個都可以。

如果它們是正確的方法,我該如何解決?如果不正確,該如何工作?

第一步應該是將新數據追加到表視圖用來填充數據的數組中

YourTablearray.append([labeltext])  

接下來更新表格數據以在表格末尾插入單元格

tableName.beginUpdates()
tableName.insertRowsAtIndexPaths([
NSIndexPath(forRow: YourTablearray.count-1, inSection: 0)], withRowAnimation: .Automatic)
tableName.endUpdates()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM