是否有关于通过从json中新添加旧数据来重新加载新数据的理论?我遇到了解决该问题的问题。我使用this infinite uitableview从我的api json字符串重新载入更多的数据。我的Api让我回到第no页的结果库。为什么我的“下拉加载数据”卡在第2页而没有加载数据
我的json头文件中包含“totalCount”,“currentPage”&“toPage”,正如它在下面的代码中所描述的:“totalCount”意味着我将得到的总结果。但是,它给了我15个结果所以,如果“totalCount”是636.我必须去43页(toPage)。
这是我的代码。
ViewController.swift
import UIKit
class ViewController: UITableViewController,AuctionAPIProtocol{
var currentPage = 1
var currentCount = 0
var toPage = 0
var totalCount = 0
var api : AuctionAPI?
let cellId = "cell"
@IBOutlet var tableViewFooter:MyFooter! //Table Footer which was activity indicator
var items:[AuctionModel] = []//An empty array where items gonna store
var newItems:[AuctionModel] = []
var loading = false // Loading State for activity indicator
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)
self.tableViewFooter.hidden = true
api = AuctionAPI(delegate: self)
api!.searchAuctionLatestFor("null",lotId: "1",page: "1")
}
//TableView Delegate and Datasource
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell
let item = items[indexPath.row]
cell.textLabel?.text = item.vendorName.capitalizedString+" "+item.modelName.capitalizedString+" "+item.year
return cell
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
// UITableView only moves in one direction, y axis
let currentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
// Change 10.0 to adjust the distance from bottom
if (maximumOffset - currentOffset) <= 40.0 {
loadSegment(currentPage, count: items.count)
}
}
func loadSegment(currentP:Int, count:Int) {
//println("load segment current page: \(currentPage)")
api = AuctionAPI(delegate: self)
if (!self.loading) {
self.setLoadingState(true)
println("CP\(currentP)")
println("TP\(count)")
if currentPage < toPage{
println("Enter Condition")
var times = 0
api!.searchAuctionLatestFor("null",lotId: "1",page: String(++currentPage))
println("Current Page After API Call : \(currentPage)")
}
else if currentPage > toPage {
setLoadingState(false)
}
}
else{
println("Not Loading")
}
}
// Loading Progress at Table View Footer condition (disabling the table view footer which include loading indicator or not)
func setLoadingState(loading:Bool) {
self.loading = loading
self.tableViewFooter.hidden = !loading
}
func didReceiveAPIResults(results: NSDictionary) {
var resultsArr: NSArray = results["body"] as NSArray
//fix
dispatch_async(dispatch_get_main_queue(), {
self.newItems = AuctionModel.latestWithJSON(resultsArr)
println("Current Auction Items : \(self.newItems.count)")
//println("Page: \(self.currentPage) Items \(self.items)")
if self.currentPage > 1 {
for item:AuctionModel in self.newItems {
self.items.append(item)
}
}
else{
self.items = self.newItems
}
self.currentCount = self.items.count
println("After Auction Items : \(self.currentCount)")
self.tableView?.reloadData()
//Status bar network activity ကို ပိတ်ရန်/ဖွင့်ခဲ့ရင်
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
self.tableView?.hidden = false
})
}
func doSearchMoreAPIResults(results: JSON){
if results != nil{
totalCount = results["header"]["totalCount"].intValue
currentPage = results["header"]["currentPage"].intValue
toPage = results["header"]["toPage"].intValue
}
println("totalCount : \(totalCount)")
println("currentPage : \(currentPage)")
println("toPage : \(toPage)")
}
func didNotReceiveAPIResults(results: Bool){
var connectionResult : Bool = results
}
}
AuctionAPI.swift
import Foundation
protocol AuctionAPIProtocol{
func didReceiveAPIResults(results: NSDictionary)
func didNotReceiveAPIResults(results: Bool)
func doSearchMoreAPIResults(results:JSON)
}
class AuctionAPI{
var delegate: AuctionAPIProtocol
var urlParameterStringController:URLParameterStringController!
init(delegate: AuctionAPIProtocol){
self.delegate=delegate
}
func post(path:String,params:Dictionary<String,String>){
//Parameter Parts.....
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
.....
self.delegate.didReceiveAPIResults(jsonData)
self.delegate.doSearchMoreAPIResults(json)
})
task.resume()
}
func searchAuctionLatestFor(token:String,lotId:String,page:String){
.....
post(urlPath,params: params)
}
}
我更新的代码,现在我停留在 “加载” 当我得到30 results.Actually它应该后向下滚动加载636结果。
这是我的控制台output。 这是我的模拟器,它在第2页并停留在加载。 Fig
现在没有更多的重复结果,但为什么它不会加载更多的数据,并在第2页时向下滚动以加载更多数据。
做一些调试,记录接收和发送的页码,告诉我们它们是什么。你需要本地化的问题,而不是只发布所有的代码,并说'找到我的问题' – Wain
我更新代码更少,更简单。我也有问题,但我不知道如何解决它。我认为有什么问题与“scrollViewDidScroll”方法用户向下滚动它通过调用“loadSegment”加载更多的结果。问题是我有30页结果的第2页,这是循环1-15和1-15并覆盖第一页1 15结果 –