ios – Swift tableView分页

前端之家收集整理的这篇文章主要介绍了ios – Swift tableView分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有成功的工作tableview与json解析代码.但可能有1000多个项目,所以需要分页时滚动底面.我不知道我如何在下面这样做我的代码.对于Objective-C有很多例子,但是对于 swift我没有找到工作示例.我等待你的帮助我想会帮助太多的人.谢谢 !
  1. import UIKit
  2.  
  3. class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
  4.  
  5. let kSuccessTitle = "Congratulations"
  6. let kErrorTitle = "Connection error"
  7. let kNoticeTitle = "Notice"
  8. let kWarningTitle = "Warning"
  9. let kInfoTitle = "Info"
  10. let kSubtitle = "You've just displayed this awesome Pop Up View"
  11.  
  12.  
  13. @IBOutlet weak var myTableView: UITableView!
  14. @IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
  15.  
  16. var privateList = [String]()
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Do any additional setup after loading the view,typically from a nib.
  21.  
  22. }
  23.  
  24. override func viewWillAppear(animated: Bool) {
  25. super.viewWillAppear(animated)
  26.  
  27. loadItems()
  28.  
  29. }
  30.  
  31. override func didReceiveMemoryWarning() {
  32. super.didReceiveMemoryWarning()
  33. // Dispose of any resources that can be recreated.
  34. }
  35.  
  36.  
  37. internal func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int
  38. {
  39. return privateList.count
  40. }
  41.  
  42.  
  43.  
  44.  
  45. internal func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  46. {
  47.  
  48. let cell:myCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! myCell
  49.  
  50. cell.titleLabel.text = privateList[indexPath.row]
  51.  
  52.  
  53. return cell
  54. }
  55.  
  56.  
  57. func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {
  58.  
  59. if (editingStyle == UITableViewCellEditingStyle.Delete){
  60.  
  61. print(indexPath.row)
  62.  
  63.  
  64. let alert = SCLAlertView()
  65. alert.addButton("Hayır"){ }
  66. alert.addButton("Evet") {
  67.  
  68. self.myTableView.beginUpdates()
  69.  
  70. self.privateList.removeAtIndex(indexPath.row)
  71. tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Left)
  72. print("Silindi")
  73.  
  74. self.myTableView.endUpdates()
  75.  
  76. self.loadItems()
  77.  
  78. }
  79. alert.showSuccess(kSuccessTitle,subTitle: kSubtitle)
  80.  
  81. }
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. func tableView(tableView: UITableView,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  91. // the cells you would like the actions to appear needs to be editable
  92. return true
  93. }
  94.  
  95.  
  96.  
  97. override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  98.  
  99.  
  100. if(segue.identifier == "Detail") {
  101.  
  102. let destinationView = segue.destinationViewController as! DetailViewController
  103.  
  104. if let indexPath = myTableView.indexPathForCell(sender as! UITableViewCell) {
  105.  
  106. destinationView.privateLista = privateList[indexPath.row]
  107.  
  108. }
  109. }
  110. }
  111.  
  112.  
  113.  
  114. internal func tableView(tableView: UITableView,estimatedHeightForHeaderInSection section: Int) -> CGFloat
  115. {
  116. return 0.0
  117. }
  118.  
  119.  
  120. func loadItems()
  121. {
  122. loadItemsNow("privateList")
  123.  
  124. }
  125.  
  126. func loadItemsNow(listType:String){
  127. myActivityIndicator.startAnimating()
  128. let listUrlString = "http://bla.com/json2.PHP?listType=" + listType + "&t=" + NSUUID().UUIDString
  129. let myUrl = NSURL(string: listUrlString);
  130. let request = NSMutableURLRequest(URL:myUrl!);
  131. request.HTTPMethod = "GET";
  132.  
  133. let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
  134. data,response,error in
  135.  
  136. if error != nil {
  137. print(error!.localizedDescription)
  138. dispatch_async(dispatch_get_main_queue(),{
  139. self.myActivityIndicator.stopAnimating()
  140. })
  141.  
  142. return
  143. }
  144.  
  145.  
  146. do {
  147.  
  148. let json = try NSJSONSerialization.JSONObjectWithData(data!,options: .MutableContainers) as? NSArray
  149.  
  150. if let parseJSON = json {
  151.  
  152.  
  153. self.privateList = parseJSON as! [String]
  154.  
  155. }
  156.  
  157. } catch {
  158. print(error)
  159.  
  160. }
  161.  
  162. dispatch_async(dispatch_get_main_queue(),{
  163. self.myActivityIndicator.stopAnimating()
  164. self.myTableView.reloadData()
  165. })
  166.  
  167.  
  168. }
  169.  
  170. task.resume()
  171. }
  172.  
  173.  
  174. }

解决方法

因此,您还需要更改服务器端.

>服务器将接受来自InDex和batchSize的API url作为查询参数.

  1. let listUrlString = "http://bla.com/json2.PHP?listType=" + listType + "&t=" + NSUUID().UUIDString + "&batchSize=" + batchSize + "&fromIndex=" + fromIndex

>在服务器响应中,会有一个额外的key totalItems.这将用于识别所有收到的项目.数组或项从fromdex到batchSize的项数.

在应用程式方面

>首先使用fromIndex = 0和batchSize = 20调用loadItem()(例如在viewDidLoad()或viewWillAppear中).在首次调用loadItem()之前,先从privateList数组中删除所有项目
>服务器返回服务器中前20个项目和totalItems项目总数的数组.
>在privateList数组中追加20个项目并重新加载tableView
>在tableView中:cellForRowAtIndexPath方法检查单元格是否是最后一个单元格.并检查totalItems(表单服务器)是否大于privateList.count.这意味着要加载的服务器中有更多的项目

  1. if indexPath.row == privateList.count - 1 { // last cell
  2. if totalItems > privateList.count { // more items to fetch
  3. loadItem() // increment `fromIndex` by 20 before server call
  4. }
  5. }

问题在哪里刷新?会滚动吗?

当收到服务器响应时,在数组中附加新项目后刷新. (步骤3)

滚动将在用户滚动时触发tableView:cellForRowAtIndexPath为每个单元格.代码正在检查它是否是最后一个单元格并获取剩余的项目. (步骤4)

示例项目添加https://github.com/rishi420/TableViewPaging

猜你在找的iOS相关文章