简体   繁体   English

TableView Load More正在不断获取

[英]TableView Load More is fetching non-stop

I setup a TableView and fetching paginated data. 我设置一个TableView并获取分页数据。 For page 1, as it's hitting route without any parameter (eg /?page=2 ). 对于第1页,因为它没有任何参数(例如/?page=2 )到达路线。 Now I am trying to implement infinite scroll kind load more. 现在,我尝试更多地实现无限滚动类型的负载。 I am trying to make a call everytime it hits the 5th row from bottom. 每当它从底部排到第5行时,我都在尝试拨打电话。

What this code does is fetches non-stop as soon as I hit the 5th cell from bottom. 该代码的作用是,一旦我从底部触到第5个单元格,便会不停地获取数据。 I think it's because the table stays on the 5th cell from bottom and keep adding cells from bottom toward up (but it may be just visual illusion) 我认为这是因为表格停留在从底部开始的第5个单元格上,并不断从底部向上添加单元格(但这可能只是视觉上的错觉)

class TableView1: UITableViewController {

  var currentPage: Int = 1
  var results: [JSON]? = []
  var urlEndpoint: String?
  var isLoading = false

  override func viewDidLoad() {
       loadItems()
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // connections to cell..

    let rowsToLoadFromBottom = 5;
    let rowsLoaded = self.results?.count   // count of fetched items

    if (!isLoading && (indexPath.row >= (rowsLoaded! - rowsToLoadFromBottom))) {
            self.loadItems()
    }

    return cell
   }   

And loadItems() function: loadItems()函数:

   func loadItems() {
      isLoading = true
      urlEndpoint = "http://appurl.app/feed"

   if currentPage != 1 {
        currentPage = currentPage! + 1
        urlEndpoint = "http://appurl.app/feed?page=\(currentPage)"
        // print(urlEndpoint)
    }

   // Alamofire call.. {
       // Error handling

      // Got results {
             self.currentPage = json["current_page"].integer
             self.currentPage = self.currentPage + 1

             self.results = data
             self.tableView.reloadData()
          }
          self.isLoading = false
      }
   }

Update : I made it fetch properly. 更新 :我正确地获取了它。 Now, I am seeing the links with page parameters on console. 现在,我在控制台上看到带有page参数的链接。 However, now it's loading all pages in a sudden (I have 6 pages, so 6 of them) so it doesn't really stop appending and I can actually see like a loop. 但是,现在它突然加载了所有页面(我有6个页面,所以其中有6个页面),因此它并没有真正停止追加,实际上我可以看到一个循环。

Update 2: 更新2:

self.currentPage = json["current_page"].int!

if self.currentPage == 1 {
   self.results = data // data is json data fetched
   self.tableView.reloadData()
} else {
   var currentCount = self.results!.count;
   let indxesPath : [NSIndexPath] = [NSIndexPath]()

   for result in data {
      self.results?.append(result)
      currentCount++
   }

   self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom)
   }                     
  self.currentPage = self.currentPage + 1
  }
  self.isLoading = false

But it's crashing on line self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom) 但是它在行self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom)上崩溃

The error: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (40) must be equal to the number of rows contained in that section before the update (20), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). 错误:无效的更新:第0节中的行数无效。更新(40)之后,现有节中包含的行数必须等于更新(20)之前,该节中包含的行数,再加上或减去从该部分插入或删除的行数(插入0,删除0),再加上或减去移入或移出该部分的行数(移入0,移出0)。

I have found my answer in another SO question . 我在另一个SO问题中找到了答案。 Here it is applied to my scenario 这里适用于我的情况

if self.currentPage == 1 {
    self.results = data
    self.tableView.reloadData()
} else {
    var currentCount = self.results!.count;
    var indxesPath : [NSIndexPath] = [NSIndexPath]()

    for result in data {
       indxesPath.append(NSIndexPath(forRow:currentCount,inSection:0));
       self.results?.append(result)
       currentCount++
    }

    self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM