5

我正在在线学习使用 Parse 作为后端创建照片共享应用程序的教程。我已经完成了两次教程,两次都是从头开始创建应用程序,但同样的错误仍然发生在同一个地方。我一直在寻找解决方案,但仍然没有运气。

我正在使用 PFQueryTableViewController,并且我的内容被加载到多个部分而不是行中。每个section在section header中显示PFUser的详细信息,每个section只有一行,显示该用户加载的图片。在每个页面的末尾(我启用了分页,一次加载 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 节中删除第 3 行,更新前仅包含 1 行'

这是我的代码:

#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

4 回答 4

2

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

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

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

- (NSIndexPath *)_indexPathForPaginationCell {

返回 [NSIndexPath indexPathForRow:0inSection:[self.objectscount]];

}

归功于一位名叫 Axel Wittmann 的先生们。

于 2015-03-24T04:30:56.580 回答
2

我想我正在和你做同样的教程。

我通过从我的项目中删除 Parse.framework,然后从“第 25 课”下载并导入 Parse.framework 来实现这一点

一定只是新的解析框架在某种程度上发生了变化

于 2014-08-16T17:27:49.873 回答
0

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

return 1;

在你的numberOfRowsInSection方法中。

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

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

于 2014-07-28T10:45:39.350 回答
0

我知道这很老,但我注意到上面给出的答案似乎并没有像输入的那样起作用。

然而,这确实有效:

- (NSIndexPath *)_indexPathForPaginationCell {
    return [NSIndexPath indexPathForRow:0 inSection:self.objects.count];    
}
于 2015-07-22T02:38:43.893 回答