PFQueryTableViewController错误

时间:2014-07-28 09:30:54

标签: ios objective-c uitableview parse-platform pfquery

我正在线上学习使用Parse作为后端创建照片共享应用程序的教程。我已经完成了两次教程,从头开始创建应用程序,并且在同一位置仍然出现相同的错误。我看起来很高很低,但仍然没有运气。

我正在使用PFQueryTableViewController,我的内容被加载到多个部分而不是行中。每个部分在部分标题中显示PFUser详细信息,每个部分只有一行,显示该用户加载的图像。在每个页面的末尾(我启用了分页,一次加载3个对象),有一个"加载更多"单击时按钮将下3个对象加载到表视图中。但是,当我单击此按钮时,我的应用程序崩溃并出现此错误:

断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-2935.137 /UITableView.m:1114 2014-07-28 01:50:37.368 SampleCamApp [25686:60b]由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'尝试删除第0部分中仅包含1行的第3行在更新之前

这是我的代码:

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // This table displays items in the Todo class
        self.parseClassName = @"Photo";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = YES;
        self.objectsPerPage = 3;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self loadObjects];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - PFQueryTableViewDataSource and Delegates
- (void)objectsDidLoad:(NSError *)error
{
    [super objectsDidLoad:error];

}

// return objects in a different indexpath order. in this case we return object based on the section, not row, the default is row
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section < self.objects.count)
    {
        return  [self.objects objectAtIndex:indexPath.section];
    }
    else
    {
        return nil;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == self.objects.count)
    {
        return nil;
    }
    static NSString *CellIdentifier = @"SectionHeaderCell";
    UITableViewCell *sectionHeaderView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    PFImageView *profileImageView = (PFImageView *)[sectionHeaderView viewWithTag:1];
    UILabel *userNameLabel = (UILabel *)[sectionHeaderView viewWithTag:2];
    UILabel *titleLabel = (UILabel *)[sectionHeaderView viewWithTag:3];

    PFObject *photo = [self.objects objectAtIndex:section];
    PFUser *user = [photo objectForKey:@"whoTook"];
    PFFile *profilePicture = [user objectForKey:@"profilePicture"];
    NSString *title = photo[@"title"];

    userNameLabel.text = user.username;
    titleLabel.text = title;

    profileImageView.file = profilePicture;
    [profileImageView loadInBackground];

    return sectionHeaderView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger sections = self.objects.count;

    if(self.paginationEnabled && sections >0)
    {
        sections++;
    }
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    if(indexPath.section == self.objects.count)
    {
        UITableViewCell *cell = [self tableView:tableView cellForNextPageAtIndexPath:indexPath];
        return cell;
    }

    static NSString *CellIdentifier = @"PhotoCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    PFImageView *photo = (PFImageView *)[cell viewWithTag:1];
    photo.file = object[@"image"];
    [photo loadInBackground];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == self.objects.count)
    {
        return 0.0f;
    }
    return 50.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == self.objects.count)
    {
        return 50.0f;
    }
    return 320.0f;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LoadMoreCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == self.objects.count && self.paginationEnabled)
    {
        [self loadNextPage];
    }
}

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    [query includeKey:@"whoTook"];

    [query orderByDescending:@"createdAt"];

    return query;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

当我运行我的应用程序时,所有内容都会正确加载,包括图像,文本等...只有当我单击按钮时才会出现故障。我现在对iOS开发非常惬意,但是,我对Parse来说还是一个新手,这是我第一次使用PFQueryTableViewController。如果我不正确地实现某些内容,请告诉我,否则我只需要解决常规的UITableViewController来实现这一点。

4 个答案:

答案 0 :(得分:2)

我想我和你一样做同样的教程。

我通过从项目中删除Parse.framework,然后从&#34;第25讲&#34;

下载并导入Parse.framework来实现此目的。

新的解析框架必须以某种方式改变

答案 1 :(得分:2)

一种解决方法是调整ParseUI中提供的自定义PFQueryTableViewController.h文件。

只需在HomeViewController.m中包含以下内容即可覆盖Parse提供的类似方法(如果它不存在,请将其添加到ParseUI.Framework&gt;标题下的PFQueryTableViewController.h中 将其添加到h。: - (NSIndexPath *)_indexPathForPaginationCell;

在.m文件中调用以下代码:

- (NSIndexPath *)_indexPathForPaginationCell {

return [NSIndexPath indexPathForRow:0inSection:[self.objectscount]];

}

以Axel Wittmann的名义向绅士们致敬。

答案 2 :(得分:0)

您的错误可能与以下内容有关:

return 1;

使用numberOfRowsInSection方法。

“loadMorePages”函数可能正在尝试删除具有“加载更多页面”按钮的行。这通常在第3行(前三行0-2是数据)。因为您更改了行和节的显示方式,所以这不起作用。

loadMorePages的文档指定它:     加载对象的下一页,附加到表,然后刷新。

答案 3 :(得分:0)

我知道这很古老,但我注意到上面给出的答案似乎没有按照键入的方式工作。

但确实有效:

- (NSIndexPath *)_indexPathForPaginationCell {
    return [NSIndexPath indexPathForRow:0 inSection:self.objects.count];    
}