2015-11-02 68 views
0

我正在写很多数据的JSON数据。有时候有几千条记录。
iOS-swift如何使分页显示表格视图?

我想使用分页显示,例如,每页20个记录。 当用户向下滚动时,我想添加下20条记录。

林发送当前页(www.exampleRequest/& pageCounter = 1)和接收 充分记录

pageCouter = 1 => 1 * 20 = 0到20的记录
pageCouter = 2 => 2 * 20 = 21到40记录

但我的问题是如何知道什么时候获得结果的下一页?

回答

0

您可以使用当前的数据计数并在查询中包含拉式计数。

var initialItems: Int = 0 //Intial Load Item 
    var currentItems: Int! //Current Items 

使用NSMutableArray添加您的数据,并为您在以前的数据或不可用的对象。

//If previous load data don't contain the same data, update the array of loaddata. 
    for object in json { 
     if !loadeddata.containsObject(object){ 
      loadeddata.insertObject(object, atIndex: 0) 
     } 
    } 
1
var pageIndex = 1 
var dataSource = [DataSourceModel]() 

func getDataSource() { 

    let parameters: Parameters = [ 
     "page": pageIndex 
    ] 
    Alamofire.request("Your URL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in 

     switch(response.result) { 
     case .success(_): 
      if let data = response.result.value{ 
       let json = JSON(data) 
       self.procssGetResponce(json: json) 
       print("response: \(json)") 
      } 
      break 

     case .failure(_): 
      print(response.result.error!) 
      break 

     } 
    } 
} 
func procssGetResponce(json: JSON) { 
//Append Elements in your object 
    //dataSource.append(ELEMENTS) 
    tblDataSource.reloadData() 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return dataSource.count + 1 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.row == dataSource.count { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "LoadCell") as! LoadMoreCell 
     cell.loadProcess.startAnimating() 
     return cell 
    } else { 
     let cell = tblDataSource.dequeueReusableCell(withIdentifier: "cellId") as! PageListCell 

     cell.dataSource = dataSource[indexPath.row] 
     cell.setDataSource() 
     return cell 
    } 
} 
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     if indexPath.row == dataSource.count { 
      pageIndex = pageIndex + 1 
      getDataSource() 
     } 
}