2015-06-09 51 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

相关问题