【问题标题】:UITableView load more when scrolling to bottomUITableView 滚动到底部时加载更多
【发布时间】:2016-04-07 22:17:57
【问题描述】:

当用户滚动到表格底部时,我想加载更多数据并创建更多单元格。我正在使用 JSON 数据和 MySql。

func dataJsonFromURL(url:String)->NSArray
{
    if let data = NSData(contentsOfURL: NSURL(string: url)!) {
        return ((try! NSJSONSerialization.JSONObjectWithData(data, options: [])) as! NSArray)
    }
    else{
        return data
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("NCell", forIndexPath: indexPath) as! TableViewCell22

    cell.backgroundColor = UIColor .clearColor()

    let mindata = (data[indexPath.row] as! NSDictionary)
}

【问题讨论】:

    标签: swift xcode uitableview


    【解决方案1】:

    使用这个库。 https://github.com/samvermette/SVPullToRefresh

    这将完全满足您的要求。 每次调用新的网络服务时,您只需要更改页码或时间戳。

    【讨论】:

    • 你可以使用Mix and Match,关注这篇文章developer.apple.com/library/ios/documentation/Swift/Conceptual/…
    • 或者如果您使用的是 cocoaPods,您可以直接使用,无需混合和匹配,只需在 viewcontroller.swift 文件顶部的 import 语句即可。
    • 这里的任何人我都需要这个 plz
    • @osamamakki 拉加载更多内容非常容易。无需使用库。你可以在上面查看我的答案
    【解决方案2】:

    它被称为 tableview 的分页。 分页用于以有效的方式在表上显示大数据。 Here's你可以得到很好的objective-c教程 并快速查看此Question。

    您可以从Here 快速下载示例。

    【讨论】:

    • 我尝试过但无法处理我的代码查看任何人都可以帮我修改它
    • 我需要一步一步来;(
    【解决方案3】:

    你需要一个非常方便的委托方法:

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell , forRowAtIndexPath indexPath: NSIndexPath) {
    
    }
    

    此方法为您提供当前显示的行。使用它提供的 indexPath 来确定您何时接近表格底部。然后获取更多 JSON 并重新加载表。

    【讨论】:

      【解决方案4】:

      你必须使用这个方法

      override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
                  let lastElement = dataSource.count - 1
                  if indexPath.row == lastElement {
                      // handle your logic here to get more items, add it to dataSource and reload tableview
                      }    
              }
      

      【讨论】:

      • 我真的很喜欢你的想法,它对我有用。但是当我查询的最后一个产品出现并且它正式成为最后一个产品时,您的代码仍在尝试带来数据并且表格视图正在闪烁。当它带来最后一个项目时,你知道如何阻止它吗?
      • @KwnstantinosNatsios 非常简单。只需添加另一个标志即可知道它是否可以加载更多。如果仍然可以加载更多 => 做我的逻辑。如果不是 => 忽略它。
      • 这个标志可能是项目的总数,但我后来想到了:D
      • 如何处理更多?
      • @SaeedRahmatolahi 是什么意思?
      【解决方案5】:

      我使用了这种方法,它对我有用

       func scrollViewDidScroll(scrollView: UIScrollView) {
          let offsetY = scrollView.contentOffset.y
          let contentHeight = scrollView.contentSize.height
      
          if offsetY > contentHeight - scrollView.frame.size.height {
      
      
              loadMoreDataFromServer()
              self.tableView.reloadData()
          }
      }
      

      【讨论】:

      • Assalamaleikum 亲爱的。为什么这段代码在 iPhone 5s 中不起作用:scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height?
      【解决方案6】:

      Xcode 8、Swift 3

      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
          let lastElement = dataSource.count - 1
          if !loadingData && indexPath.row == lastElement {
              indicator.startAnimating()
              loadingData = true
              loadMoreData()
          }
      }
      

      【讨论】:

      • swift 不允许我成功运行应用程序,因为 loadingData loadMoreData 它显示错误“使用未解析的标识符”
      • @Saeed Rahmatolahi 检查单元格标识符是否正确分配并在 UITableview 中使用
      • 你能编辑这个代码吗?我的表格视图单元格中有一些文本,它们将通过 json 获得
      • @AnupGupta dataSource 只不过是你的数据数组,表格视图获取它的值
      • LoadingData 是他们创建的一个布尔标志,用于跟踪何时加载。如果您没有此变量,则它是“未解析的标识符”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多