在tableview重新加载后,PFQueryTableViewController应用程序崩溃

时间:2014-09-21 09:16:15

标签: ios uitableview parse-platform

在我的应用程序中,我有一个显示会议列表的PFQueryTableViewController。点击自定义表格视图单元格会推送会议本身的详细视图,在此视图中,用户可以通过按钮取消会议。我设法在表视图中实现两个部分,一个用于由当前用户创建的会议,另一个用于其他部分。表视图工作正常并正确提取我的解析数据,但如果我进入详细视图并取消会议,然后返回表视图,应用程序将终止并显示错误。同样的事情,如果我只是删除解析Core中的一个会议,然后拉动刷新表视图。错误是这样的:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -      [__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010c4703f5 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010c109bb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010c35b4d3 -[__NSArrayM objectAtIndex:] + 227
3   SpeedMeet_1.0                       0x0000000107d28fbc -[PFQueryTableViewController tableView:cellForRowAtIndexPath:] + 167
4   UIKit                               0x000000010aabdcd3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
5   UIKit                               0x000000010aa9d7f1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
6   UIKit                               0x000000010aab365c -[UITableView layoutSubviews] + 213
7   UIKit                               0x000000010aa40199 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
8   QuartzCore                          0x0000000109b60f98 -[CALayer layoutSublayers] + 150
9   QuartzCore                          0x0000000109b55bbe _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
10  QuartzCore                          0x0000000109b55a2e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
11  QuartzCore                          0x0000000109ac3ade _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
12  QuartzCore                          0x0000000109ac4bea _ZN2CA11Transaction6commitEv + 390
13  QuartzCore                          0x0000000109ac5255 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
14  CoreFoundation                      0x000000010c3a5347 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
15  CoreFoundation                      0x000000010c3a52a0 __CFRunLoopDoObservers + 368
16  CoreFoundation                      0x000000010c39b0d3 __CFRunLoopRun + 1123
17  CoreFoundation                      0x000000010c39aa06 CFRunLoopRunSpecific + 470
18  GraphicsServices                    0x000000010c9859f0 GSEventRunModal + 161
19  UIKit                               0x000000010a9c7550 UIApplicationMain + 1282
20  SpeedMeet_1.0                       0x0000000107cec163 main + 115
21  libdyld.dylib                       0x000000010ccdc145 start + 1
22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我在viewWillAppear中调用的loadobject方法应该清除表视图然后重新加载它的数据,所以我不明白为什么会发生这种情况。 这是我的实现文件:

@interface SMConfirmedMeetingsTableViewController ()

@property (strong,nonatomic) NSMutableArray *dataArray;
@property (strong,nonatomic) NSMutableArray *firstSectionArray;
@property (strong,nonatomic) NSMutableArray *secondSectionArray;

@end

@implementation SMConfirmedMeetingsTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
    // Custom the table

    // The className to query on
    self.parseClassName = @"MeetingObject";

    // The title for this table in the Navigation Controller.
    self.title = @"Confirmed meetings";

    // Whether the built-in pull-to-refresh is enabled
    self.pullToRefreshEnabled = YES;

    // Whether the built-in pagination is enabled
    self.paginationEnabled = YES;

    // The number of objects to show per page
    self.objectsPerPage = 20;
}
return self;
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

UIBarButtonItem *menuIcon = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)];
self.navigationItem.leftBarButtonItem = menuIcon;

UINib *nib = [UINib nibWithNibName:@"SMShowMeetingTableViewCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"SMShowMeetingTableViewCell"];
}

-(void)viewWillAppear:(BOOL)animated
{
[self loadObjects];
}


#pragma mark - Parse

- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
if (error) {
    // something
}
// This method is called every time objects are loaded from Parse via the PFQuery

PFUser *currentUser = [PFUser currentUser];

self.dataArray = [[NSMutableArray alloc] init];
self.firstSectionArray = [[NSMutableArray alloc] init];
self.secondSectionArray = [[NSMutableArray alloc] init];

for (PFObject *object in self.objects) {
    NSString *author = [object objectForKey:@"author"];
    if ([author isEqualToString: currentUser.username]) {
        //first section
        [self.firstSectionArray addObject:object];
    }else{
        //second section
        [self.secondSectionArray addObject:object];
    }
}

//add firstsection to the data array
NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:self.firstSectionArray forKey:@"data"];
[self.dataArray addObject:firstItemsArrayDict];

//add Secondsection to the data array
NSDictionary *secondItemsArrayDict = [NSDictionary dictionaryWithObject:self.secondSectionArray forKey:@"data"];
[self.dataArray addObject:secondItemsArrayDict];
}

- (PFQuery *)queryForTable {

PFUser *currentUser = [PFUser currentUser];

PFQuery *queryAsAuthor = [PFQuery queryWithClassName:self.parseClassName];
[queryAsAuthor whereKey:@"user" equalTo: currentUser];

PFQuery *queryAsParticipant = [PFQuery queryWithClassName:self.parseClassName];
[queryAsParticipant whereKey:@"participants" equalTo:currentUser.username];

PFQuery *query = [PFQuery orQueryWithSubqueries:@[queryAsAuthor, queryAsParticipant]];
[query whereKey:@"isAvailable" equalTo:[NSNumber numberWithBool:NO]];
[query orderByAscending:@"meetingDateAndTime"];

return query;
}


# pragma mark - Table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
    return self.firstSectionArray.count;
}else
    return self.secondSectionArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// sections title text
if (section==0) {
    return @"Created by me";
}else return @"Created by others";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"SMShowMeetingTableViewCell";

SMShowMeetingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[SMShowMeetingTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

NSDictionary *dictionary = [self.dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"data"];
PFObject *obj = [array objectAtIndex:indexPath.row];

cell.categoryLabel.text = [obj objectForKey:@"meetingCategoryString"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
cell.dateLabel.text = [dateFormatter stringFromDate:[obj objectForKey:@"meetingDateAndTime"]];
cell.locationLabel.text = [obj objectForKey:@"meetingLocationString"];

return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"NextPage";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = NSLocalizedString(@"Loading more meetings...",@"loading");
cell.userInteractionEnabled = NO;

return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didSelectRowAtIndexPath:indexPath];

NSDictionary *dictionary = [self.dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"data"];
PFObject *obj = [array objectAtIndex:indexPath.row];
SMShowMeetingDetailViewController *meetingDetail = [[SMShowMeetingDetailViewController alloc]init];
meetingDetail.isMyMeetings = YES;
meetingDetail.meeting = obj;
[self.navigationController pushViewController:meetingDetail animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentSize.height - scrollView.contentOffset.y < (self.view.bounds.size.height)) {
    if (![self isLoading]) {
        [self loadNextPage];
    }
}
}

@end

1 个答案:

答案 0 :(得分:0)

您需要做的是将[self.tableView reloadData];添加到objectsDidLoad:的末尾。这将使用您更新的数据刷新UI。