我使用parse.com框架與Swift和PFQueryTableViewController時,我設置分頁它將無法正常工作。如果數據庫的行數少於objectPerPage中設置的數量,那麼它工作正常,但如果有更多的行,並且當我運行應用程序時,它會一直顯示加載屏幕,並且沒有任何內容被下載,當我刷新時刷新它會崩潰 錯誤PFQueryTableViewController分頁不適用於heightForRowAtIndexPath
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]
ImagesTableViewController.swift
import UIKit
import Parse
import ParseUI
import Bolts
class ImagesTableViewController: PFQueryTableViewController {
@IBAction func unwindToSegue (segue : UIStoryboardSegue) {}
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Image"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 5
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Image")
query.whereKey("deleted", notEqualTo: 1)
query.orderByDescending("createdAt")
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("ImageCell") as! ImageTVCell!
if cell == nil {
cell = ImageTVCell(style: UITableViewCellStyle.Default, reuseIdentifier: "ImageCell")
}
// Extract values from the PFObject to display in the table cell HEADLINE
if let caption = object?["caption"] as? String {
cell?.headlineLabel?.text = caption
}
// Display image
var initialThumbnail = UIImage(named: "question")
cell.postImageView.image = initialThumbnail
if let thumbnail = object?["image"] as? PFFile {
cell.postImageView.file = thumbnail
cell.postImageView.loadInBackground()
}
return cell
}
// if I remove this code pagination work but the cell height is wrong
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return calculateHeightForRowAtIndexPath(indexPath)
}
func calculateHeightForRowAtIndexPath(indexPath: NSIndexPath) -> CGFloat {
if let ratio = objectAtIndexPath(indexPath)?["aspect"] as? Float {
println("Ratio: \(ratio)")
return tableView.bounds.size.width/CGFloat(ratio)
} else {
return 50.0
}
}
@IBAction func addNewPhotoButton(sender: UIBarButtonItem) {
self.tabBarController?.tabBar.hidden = true
self.performSegueWithIdentifier("showUploadNewImage", sender: self)
}
}
謝謝你的回覆,我說是正確的:如果我將用「刷新」功能,我還可以設置手動創建小區'self.paginationEnabled = FALSE' ? 的事情是,當我刪除 '覆蓋FUNC的tableView(的tableView:UITableView的,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat的{ 回報calculateHeightForRowAtIndexPath(indexPath) }' 的應用程序運行完美,但細胞高度搞砸,最後一個單元格是單元格Load more。我不明白的是,** heightForRowAtIndexPath **正在計算單元格高度,所以爲什麼它禁用分頁... –
你必須插入上面的方法的代碼片段之間的其他方法。我將編輯答案來反映這一點。 – Kumuluzz
測試您發佈的代碼是我做的第一件[屏幕截圖](https://www.anony.ws/image/D6cZ),但加載更多數據的最後一個單元未顯示... –