【问题标题】:Reload TableView only at new added Cells仅在新添加的单元格处重新加载 TableView
【发布时间】:2020-03-17 23:13:57
【问题描述】:

我有一个 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 compilation error

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

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

我该怎么办,谢谢?

【问题讨论】:

    标签: swift uitableview reload batch-fetching


    【解决方案1】:

    您需要做的是计算需要重新加载的行,并确保在使用新数据更新数据源后将这些 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) }
    }
    

    【讨论】:

    • 我正在尝试了解您的功能。我有一个包含 10 个元素的 arrayA。我运行我的批量获取并添加了 10 个以上的元素。 arrayA 有 20 个元素。然后我使用 arrayA.count - 新的 items.count 来获取起始索引。然后你想让我拿 startIndex + newItems.count 但我想我可以拿 arrayA.count - 1 作为结束索引。然后你想让我映射到返回结果。我真的不明白结果。如何将此结果实现到 reloadRowsAtIndexPath 函数中?
    • 如果我得到零个新元素怎么办。如果我得到零个新元素,startIndex 将为 10,但我的数组中没有第 10 个索引元素,所以我的应用程序会崩溃。
    • 我想说的是,您不需要使用该方法重新加载整个表视图,只需重新加载新行,并且仅当有任何新行需要重新加载时。你的旧行应该没问题。
    【解决方案2】:

    这是我添加的补充: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()
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多