代码之家  ›  专栏  ›  技术社区  ›  ap123

Tableview插入行跳跃

  •  0
  • ap123  · 技术社区  · 6 年前

    我已经在表视图中实现了insertRows方法。如果在tableView插入新行时向下滚动或滚动到tableView内容的底部,则整个表格会出现跳跃,并将我放置在tableView内容的不同位置。如何修复此问题?我在底部添加了代码:

    //Method to load more data
    func loadMoreFeed() {
        let rowsBefore = self.tableObjects.count
        loadMoreFeedInBackground() { queryObjects in
            for object in queryObjects! {
                self.tableObjects.append(object)
            }
    
            self.tableObjects.sort {
                $0.date!.compare($1.date!) == .orderedDescending 
            }
    
            let rowsAfter = self.tableObjects.count
            var indexPaths = [IndexPath]()
    
            for i in 0 ... (rowsAfter - rowsBefore - 1){ //start i at zero and go up to the difference - 1
                indexPaths.append(IndexPath(row: rowsBefore + i, section: 0)) //insert an index path in the array
            }
    
            self.isMoreDataLoading = false
            self.loadingMoreView!.stopAnimating()
    
            self.tableView.beginUpdates()
            self.tableView.insertRows(at: indexPaths, with: .automatic)
            self.tableView.endUpdates()
        }            
    }
    

    加载新对象的时间方法

    var isMoreDataLoading = false
    var loadingMoreView : InfiniteScrollActivityView?
    
    /* Loads more feed while scrolling */
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        if (!isMoreDataLoading) { //is not loading more
    
            // Calculate the position of one screen length before the bottom of the tableview
            let scrollViewContentHeight = tableView.contentSize.height
            let scrollOffsetThreshold = scrollViewContentHeight - tableView.bounds.size.height
    
            //start loading more data after user went 4/5 of the way through the content
            if(scrollView.contentOffset.y > 4*scrollOffsetThreshold/5 && tableView.isDragging) {
    
                isMoreDataLoading = true //set loading data to true
    
                // Update position of loadingMoreView, and start loading indicator
                let frame = CGRect(x: 0, y: tableView.contentSize.height, width: tableView.bounds.size.width, height: InfiniteScrollActivityView.defaultHeight)
                loadingMoreView?.frame = frame
                loadingMoreView!.startAnimating()
                loadMoreFeed()
    
            }                
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   iAmrSalman    6 年前

    对于何时加载新对象的方法,这是我的实现

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {    
      let lastSectionIndex = tableView.numberOfSections - 1
      let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
      if indexPath.section ==  lastSectionIndex && indexPath.row == lastRowIndex {
        // print("this is the last cell")
    
        if (!isMoreDataLoading) { //is not loading more
          // start loading indicator
          loadMoreFeed()
    
          // Update position of loadingMoreView, and 
          loadingMoreView?.frame = CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: InfiniteScrollActivityView.defaultHeight)
          loadingMoreView!.startAnimating()
    
           self.tableView.tableFooterView = loadingMoreView
           self.tableView.tableFooterView?.isHidden = false
        }
      }
    }
    


    为了更新tableView,这是我的实现

    private func updateTableView() {
      let rowsBefore = self.tableObjects.count
      loadMoreFeedInBackground() { queryObjects in
      for object in queryObjects! {
        self.tableObjects.append(object)
      }
    
      self.tableObjects.sort {
        $0.date!.compare($1.date!) == .orderedDescending 
      }
    
      let rowsAfter = self.tableObjects.count
      let indexes = rowsAfter - rowsBefore
    
      guard indexes > 0 else {
        self.isMoreDataLoading = false
        self.loadingMoreView!.stopAnimating()
        self.tableView.tableFooterView?.isHidden = true
        return
      }
    
      var indexPaths = [IndexPath]()
      for index in 0...(indexes - 1) {
        let indexPath = IndexPath(row: index + rowsBefore, section: 0)
        indexPaths.append(indexPath)
      }
    
      if tableView.contentOffset.y > tableView.estimatedRowHeight {
        let initialOffset = self.tableView.contentOffset
    
        tableView.beginUpdates()
        tableView.insertRows(at: indexPaths, with: .fade)
        tableView.setContentOffset(initialOffset, animated: false)
        tableView.endUpdates()
    
      } else {
        tableView.insertRows(at: indexPaths, with: .fade)
      }
    
      //refreshControl.endRefreshing()
    }