我有成功的工作tableview与json解析代码.但可能有1000多个项目,所以需要分页时滚动底面.我不知道我如何在下面这样做我的代码.对于Objective-C有很多例子,但是对于
swift我没有找到工作示例.我等待你的帮助我想会帮助太多的人.谢谢 !
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()
}
}
解决方法
因此,您还需要更改服务器端.
>服务器将接受来自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