【发布时间】:2015-05-04 08:34:37
【问题描述】:
是否有关于通过将新数据附加到 json 中的新数据来重新加载新数据的理论?我在解决该案例时遇到问题。我使用 this infinite uitableview 从我的 api json 字符串中重新加载更多数据。我的 Api 给我返回结果基于页码。
我的 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 个结果后向下滚动时卡在“正在加载”。实际上它应该加载 636 个结果。
这是我的控制台output。 这是我的模拟器,它在第 2 页并卡在加载中。Fig
现在没有更多重复的结果,但是为什么当我向下滚动以加载更多时它不加载更多数据并停留在第 2 页。
【问题讨论】:
-
做一些调试,记录接收和发送的页码,告诉我们它们是什么。您需要本地化问题,而不是只发布所有代码并说“找到我的问题”
-
我将代码更新为更少更简单。我也遇到了问题,但我不知道如何解决它。我认为“scrollViewDidScroll”方法有问题,用户向下滚动它会加载更多结果通过调用“loadSegment”。问题是我有第 2 页的 30 个结果循环 1-15 和 1-15 并覆盖第一页 1 15 结果
标签: ios uitableview swift uiscrollview infinite-scroll