我可以使用json解析代码成功工作tableview。但是可能还有1000多个项目,因此在滚动底部时需要分页。我不知道如何在下面执行我的代码。对于Objective-C,有很多例子,但是为了迅速,我没有找到有效的例子。我在等你的帮助。我认为会帮助太多人。谢谢 !
import UIKit class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate { let kSuccessTitle = "Congratulations" let kErrorTitle = "Connection error" let kNoticeTitle = "Notice" let kWarningTitle = "Warning" let kInfoTitle = "Info" let kSubtitle = "You've just displayed this awesome Pop Up View" @IBOutlet weak var myTableView: UITableView! @IBOutlet weak var myActivityIndicator: UIActivityIndicatorView! var privateList = [String]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) loadItems() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return privateList.count } internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:myCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! myCell cell.titleLabel.text = privateList[indexPath.row] return cell } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if (editingStyle == UITableViewCellEditingStyle.Delete){ print(indexPath.row) let alert = SCLAlertView() alert.addButton("Hayır"){ } alert.addButton("Evet") { self.myTableView.beginUpdates() self.privateList.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left) print("Silindi") self.myTableView.endUpdates() self.loadItems() } alert.showSuccess(kSuccessTitle, subTitle: kSubtitle) } } func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { // the cells you would like the actions to appear needs to be editable return true } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if(segue.identifier == "Detail") { let destinationView = segue.destinationViewController as! DetailViewController if let indexPath = myTableView.indexPathForCell(sender as! UITableViewCell) { destinationView.privateLista = privateList[indexPath.row] } } } internal func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { return 0.0 } func loadItems() { loadItemsNow("privateList") } func loadItemsNow(listType:String){ myActivityIndicator.startAnimating() let listUrlString = "http://bla.com/json2.php?listType=" + listType + "&t=" + NSUUID().UUIDString let myUrl = NSURL(string: listUrlString); let request = NSMutableURLRequest(URL:myUrl!); request.HTTPMethod = "GET"; let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { print(error!.localizedDescription) dispatch_async(dispatch_get_main_queue(),{ self.myActivityIndicator.stopAnimating() }) return } do { let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray if let parseJSON = json { self.privateList = parseJSON as! [String] } } catch { print(error) } dispatch_async(dispatch_get_main_queue(),{ self.myActivityIndicator.stopAnimating() self.myTableView.reloadData() }) } task.resume() } }
为此,您还需要更改服务器端。
服务器将接受fromIndex并batchSize在APIurl中作为查询参数。
fromIndex
batchSize
API
let listUrlString = "http://bla.com/json2.php?listType=" + listType + "&t=" + NSUUID().UUIDString + "&batchSize=" + batchSize + "&fromIndex=" + fromIndex
在服务器响应中,将有一个额外的密钥totalItems。这将用于识别是否已收到所有项目。阵列或项目fromIndex到batchSize项目数。
totalItems
在应用程序方面
首先loadItem()将用fromIndex = 0和调用batchSize = 20(例如,在viewDidLoad()或中viewWillAppear)。第一次privateList调用之前从数组中删除所有项目loadItem()
loadItem()
fromIndex = 0
batchSize = 20
viewDidLoad()
viewWillAppear
privateList
服务器返回服务器中前20个项目和项目totalItems总数的数组。
追加privateList数组中的20个项目并重新加载tableView
tableView
在tableView:cellForRowAtIndexPath方法中,检查该单元格是否为最后一个单元格。并检查totalItems(表单服务器)是否大于privateList.count。这意味着服务器中还有更多项目要加载
tableView:cellForRowAtIndexPath
privateList.count
if indexPath.row == privateList.count - 1 { // last cell if totalItems > privateList.count { // more items to fetch loadItem() // increment `fromIndex` by 20 before server call }
}
题: where is refresh ? will be scrolling ?
where is refresh ? will be scrolling ?
收到服务器响应后,在数组中添加新项目后刷新。(第3步)
tableView:cellForRowAtIndexPath用户滚动时,滚动将触发每个单元格。代码正在检查它是否是最后一个单元格并获取剩余的项目。(第4步)
添加了示例项目:https : //github.com/rishi420/TableViewPaging