2015-06-09 33 views
0

我使用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) 
} 

} 

回答

1

發生由於PFQueryTableViewController的從實施方法tableView:numberOfRowsInSection的此問題。我複製/從GitHub repo containing PFQueryTableViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSInteger count = [self.objects count]; 
    if ([self _shouldShowPaginationCell]) { 
     count += 1; 
    } 
    return count; 
} 

它簡單地返回一個對象,以顯示(這是有道理的)的計數粘貼它,但如果啓用了分頁,那麼它需要將顯示一個額外的電池。這意味着你必須用文本「加載更多數據」或類似的東西手動創建另一個單元格,這會觸發刷新。


克服這種情況的方法是簡單地拿自己與下面覆蓋tableView:numberOfRowsInSection

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.objects!.count 
} 

更新1

預建Parse分頁按鈕不見了以前的答案


使用以下代碼段,用於計算單元格的高度,以顯示預置的Parse分頁按鈕

func calculateHeightForRowAtIndexPath(indexPath: NSIndexPath) -> CGFloat { 
    // Special case for pagination, using the pre-built one by Parse 
    if (indexPath.row >= objects!.count) { return 50.0 } 

    // Determines the height if an image ratio is present 
    if let ratio = objectAtIndexPath(indexPath)?["aspect"] as? Float { 
     println("Ratio: \(ratio)") 
     return tableView.bounds.size.width/CGFloat(ratio) 
    } else { 
     return 50.0 
    } 
} 
+0

謝謝你的回覆,我說是正確的:如果我將用「刷新」功能,我還可以設置手動創建小區'self.paginationEnabled = FALSE' ? 的事情是,當我刪除 '覆蓋FUNC的tableView(的tableView:UITableView的,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat的{ 回報calculateHeightForRowAtIndexPath(indexPath) }' 的應用程序運行完美,但細胞高度搞砸,最後一個單元格是單元格Load more。我不明白的是,** heightForRowAtIndexPath **正在計算單元格高度,所以爲什麼它禁用分頁... –

+1

你必須插入上面的方法的代碼片段之間的其他方法。我將編輯答案來反映這一點。 – Kumuluzz

+0

測試您發佈的代碼是我做的第一件[屏幕截圖](https://www.anony.ws/image/D6cZ),但加載更多數據的最後一個單元未顯示... –

0

使用解析1.11與iOS 9.2和7.2的Xcode解析分頁完美地工作。 當用戶覆蓋Parse自身使用的某些funcs時未正確管理Parse添加的「加載更多...」行時出現問題。 在我的情況下,我需要重寫tableView-canEditRowAtIndexPath以確定當前用戶是否可以根據對象的ACL刪除行。 我最初的FUNC是:

覆蓋FUNC的tableView(的tableView:UITableView的,canEditRowAtIndexPath indexPath:NSIndexPath) - >布爾{

if let curUser = PFUser.currentUser() { 
     let currentObject = objects![indexPath.row] 
     if let acl = currentObject.ACL { 
      return acl.getWriteAccessForUser(curUser) 
     } else { 
      return true 
     } 
    } 
    return true 
} 

但我得到indexpath除外出界,當負載更多線是在列表滾動期間遇到。 問題解決了添加此測試:

if (indexPath.row == self.objects!.count) { // row "Load More ..." 
     return true 
    } 

沒有這個代碼「加載更多...」沒有被解析添加的行! 所以完全正確的首要FUNC是:

覆蓋FUNC的tableView(的tableView:UITableView的,canEditRowAtIndexPath indexPath:NSIndexPath) - >布爾{

if (indexPath.row == self.objects!.count) { // row "Load More ..." 
     return true 
    } 
    if let curUser = PFUser.currentUser() { 
     let currentObject = objects![indexPath.row] 
     if let acl = currentObject.ACL { 
      return acl.getWriteAccessForUser(curUser) 
     } else { 
      return true 
     } 
    } 
    return true 
} 

一般來說所有重載funcs中包括heightForRowAtIndexpath,必須採取的護理啓用分頁功能時,由Parse添加額外的行。

HTH

羅伯託的Targa

相關問題