import UIKitclass 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,estimatedHeightFor@R_301_5548@InSection 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() }}解决方法 因此,您还需要更改服务器端.
>服务器将接受来自InDex和batchSize的API url作为查询参数.
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.这意味着要加载的服务器中有更多的项目
if indexPath.row == privateList.count - 1 { // last cell if totalitems > privateList.count { // more items to fetch loadItem() // increment `fromIndex` by 20 before server call }}
问题在哪里刷新?会滚动吗?
当收到服务器响应时,在数组中附加新项目后刷新. (步骤3)
滚动将在用户滚动时触发tableVIEw:cellForRowAtIndexPath为每个单元格.代码正在检查它是否是最后一个单元格并获取剩余的项目. (步骤4)
示例项目添加:https://github.com/rishi420/TableViewPaging
总结以上是内存溢出为你收集整理的ios – Swift tableView分页全部内容,希望文章能够帮你解决ios – Swift tableView分页所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)