我已经通过一种方法搜索了swift和parse(parse.com)的分页并且没有找到我想要的东西,所以在解决方案上工作并想分享它。它于今天09/22/2015进行了测试和工作,包括Xcode 7,iOS9和解析库1.8.2:
在这种类型的分页中,所有数据都存储在tableDataArray
中,永远不会被删除。如果没有更多数据要下载,变量tableDataArray
会保持您的尺寸不变。
首先在ViewController Class
上定义这些属性:
@IBOutlet weak var myTableView: UITableView!
var tableDataArray = [String]()
var countPage = 0 //number of current page
var stepPage = 50 //number of records by page
var maxRow = 0 //maximum limit records of your parse table class
var maxPage = 0 //maximum page
//Start the process to load the data
override func viewDidAppear(animated: Bool)
{
self.tableDataArray.removeAll()
getLimitRecordFromTable("parseClassName")
}
覆盖或声明TableView Protocol
的此功能:
//Pagination
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
let row = indexPath.row
let lastRow = self.tableGamesArray.count - 1
let pageLimit = (((self.countPage+1) * (self.stepPage)) - 1) //prevision of the page limit based on step and countPage
//Only for debug
print("\(row) from [0,\(lastRow)] -> limit: \(pageLimit)")
// 1) The last rown and is the last
// 2) To avoid two calls in a short space from time, while the data is downloading
// 3) The current array count is smaller than the maximum limit from database
if (row == lastRow) && (row == pageLimit) && (self.tableGamesArray.count < self.maxRow)
{
self.countPage++
print("Loading Page \(self.countPage) from \(self.maxPage)")
self.loadGamesFromCloudDataStore(false)
self.myTableView.reloadData()
}
}
获取解析表类的最大限制记录:
func getLimitRecordFromTable(parseClassName: String)
{
let queryLocal = PFQuery(className: parseClassName)
queryLocal.countObjectsInBackgroundWithBlock(
{
(countLocal: Int32, errorLocal: NSError?) -> Void in
if( errorLocal == nil)
{
self.maxRow = Int(countLocal) //Limit for pagination
self.maxPage = Int(self.maxRow/self.stepPage) + 1
self.loadGamesFromCloudDataStore()
}
})
}
从解析数据云商店加载游戏:
func loadGamesFromCloudDataStore(parseClassName: String)
{
let query = PFQuery(className: parseClassName)
//Pagination
query.limit = self.stepPage
if( self.countPage > 0 )
{
query.skip = self.countPage * self.stepPage
}
query.findObjectsInBackgroundWithBlock(
{
(objects, error) -> Void in
if let objects = objects
{
for object in objects
{
let id = object.objectId as String!
self.tableDataArray.append(id)
}
self.myTableView.reloadData()
}
})
}