0

我正在尝试将新数据附加到 Tableview,并且只为添加的新数据重新加载 Tableview。

我的应用程序一直崩溃给我这个错误:“尝试从第 0 部分删除第 19 行,它在更新前只包含 10 行”

一旦异步函数 contentQueryContinuous() 完成,我想重新加载这些行。

这是我的代码:

//Once User Scrolls all the way to bottom, beginContentBatchFetch() is called

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentSize.height
    let contentHeight = scrollView.contentSize.height
    if offsetY > contentHeight - scrollView.frame.height {
        beginContentBatchFetch()
    }
}

//Content Batch Fetch looks up to 10 new elements, tries to append to current array's, and then reload's tableview only for those new elements

func beginContentBatchFetch() {
    contentFetchMore = true
    ProgressHUD.show()
    let oldcount = contentObjectIdArray.count
    var IndexPathsToReload = [IndexPath]()
    var startIndex = Int()
    var endIndex = Int()
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  //Calls self.contentQueryContinous which adds new elements
        self.contentQueryContinous()
        let newElements = self.contentObjectIdArray.count - oldcount
        startIndex = oldcount
        endIndex = self.contentObjectIdArray.count
        for index  in startIndex..<endIndex {
            let indexPath = IndexPath(row: index, section: 0)
            IndexPathsToReload.append(indexPath)
            }
        if newElements > 0 {
        self.MyTableView.reloadRows(at: IndexPathsToReload, with: .fade)
        }
        ProgressHUD.dismiss()
      }
  }

这是 contentQueryContinous()

   func contentQueryContinous() {
    if contentObjectIdArray.count != 0 {
        contentSkip = contentObjectIdArray.count
        let query = PFQuery(className: "ContentPost")
        query.whereKey("Spot", equalTo: SpotText)
        query.limit = 10
        query.skip = contentSkip
        query.addDescendingOrder("createdAt")
        query.findObjectsInBackground(block: { (objects: [PFObject]?,error: Error?) in
        if let objects = objects {
            for object in objects {
                    let ProfileImageFile = object["ProfileImage"] as? PFFileObject
                            let urlString = ProfileImageFile?.url as! String
                                        if let url = URL(string: urlString) {
                                            let data = try? Data(contentsOf: url)
                                               if let imageData = data {
                                                   self.contentPostProPicUrlArray.append(urlString as NSString)
                                                   self.contentPostProPicImageCache.setObject(UIImage(data: imageData)!, forKey: urlString as NSString)
                                               }
                                           }
                    if object["Post"] != nil && object["UserLikes"] != nil && object["Username"] != nil && object["UserTime"] != nil {
                        self.contentPostArray.append(object["Post"] as! String)
                        self.contentLikeArray.append(object["UserLikes"] as! Int)
                        self.contentUsernameArray.append(object["Username"] as! String)
                        self.contentTimeArray.append(object["UserTime"] as! String)
                        self.contentObjectIdArray.append(object.objectId!)
              }
            }
            print(self.contentPostArray)
        }
    })
}

}

4

1 回答 1

0

您正在尝试重新加载不在桌面上的单元格。尝试插入它。此外,您还冒着竞争条件的风险。

文档

重新加载一行会导致表视图向其数据源询问该行的新单元格。该表在为旧行设置动画时为该新单元格设置动画。如果要提醒用户单元格的值正在更改,请调用此方法。

于 2020-04-25T18:51:55.403 回答