我正在在线学习使用 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 即可实现这一点。