0

我有一个 scrollToBottom 函数,它通知应用程序何时 beginBatchFetches 获取内容、图片和检查。

   //1: User Scrolls all the way down calls beginBatchFetch()
   func scrollViewDidScroll(_ scrollView: UIScrollView) {
         let offsetY = scrollView.contentOffset.y
         let contentHeight = scrollView.contentSize.height
         print("offsetY: \(offsetY)")
         if offsetY > contentHeight - scrollView.frame.height {
            if self.viewSelected == "Content" {
                if !contentFetchMore {
                    beginContentBatchFetch()
                }
            } else if self.viewSelected == "Pics" {
                    if !picFetchMore {
                        beginPicBatchFetch()
                    }
            } else if self.viewSelected == "Checks" {
                        if !checksFetchMore {
                            beginChecksBatchFetch()
                        }
                    }
            }

     }

这些是 beginBatchFetchMethods。Self.reload 当前重新加载整个 Tableview:

 func beginContentBatchFetch() {
    contentFetchMore = true
    ProgressHUD.show()
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        self.checkQueryContinous()

        ProgressHUD.dismiss()
    }

}




func beginPicBatchFetch() {
     picFetchMore = true
           ProgressHUD.show()
           DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.PicContinous()
            self.Reload()
            ProgressHUD.dismiss()
           }

}

func beginChecksBatchFetch() {
    checksFetchMore = true
           ProgressHUD.show()
           DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.checkQueryContinous()
            self.Reload()
            ProgressHUD.dismiss()
           }

}

而不是重新加载整个 TableView。我只想重新加载新添加的单元格。例如,如果我在批量获取之前有 10 个单元格,然后在批量获取之后有 20 个单元格,我只想为这些单元格 11-20 重新加载表格视图。

我在网上看了看,StackOverFlow 把我带到了这个:iOS Swift and reloadRowsAtIndexPaths 编译错误

      var _currentIndexPath:NSIndexPath?
      if let indexPath = _currentIndexPath {
     self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: 
     UITableViewRowAnimation.None)
      }
      else {
 `    //_currentIndexPath` is nil.
      // Error handling if needed.
    }

据我了解,这只会重新加载显示的单元格。但是,我的批处理可能不会返回更多值。结果,我的 tableview 将显示与批量获取之前相同的单元格,这将重新加载这些单元格。由于这些单元格之前已加载,我不想浪费数据/搞砸我的表格视图。

我该怎么办,谢谢?

4

2 回答 2

0

这是我添加的补充:https ://www.youtube.com/watch?v=whbyVPFFh4M

我可能搞砸了 for in 循环功能。我还不太了解映射。

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) {
        self.checkQueryContinous()
        let newElements = self.contentObjectIdArray.count - oldcount
        startIndex = self.contentObjectIdArray.count - newElements
        endIndex = self.contentObjectIdArray.count
        for index  in startIndex..<endIndex {
            let indexPath = IndexPath(row: index, section: 0)
            IndexPathsToReload.append(indexPath)
        }
        if newElements > 0 {
        self.BarSelectedTableView.reloadRows(at: IndexPathsToReload, with: .fade)
    }
        ProgressHUD.dismiss()
    }

}
于 2019-11-22T01:31:04.973 回答
0

您需要做的是计算需要重新加载的行,并确保在使用新数据更新数据源后将这些 indexPaths 传递到重新加载行函数。

 private func calculateIndexPathsToReload(from newItems: [Items]) -> [IndexPath] {
    let startIndex = results!.count - newItems.count
    let endIndex = startIndex + newItems.count
    return (startIndex..<endIndex).map { IndexPath(row: $0, section: 0) }
}
于 2019-11-22T00:10:24.257 回答