2014-09-21 59 views
0

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

*** 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中应该明确表视图,然后重新加载它的数据,所以我不明白为什么会这样。 这是我实现文件:

@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

尝试在'objectsDidLoad:'的末尾添加'[self.tableView reloadData];'。 – Dehli 2014-09-21 18:53:55

+1

它的工作原理!所以基本上没有这行视图控制器试图重新加载相同的表(我的意思是相同数量的部分和行),但与不同的阵列...非常感谢,我疯了XD – Diego 2014-09-21 19:37:11

回答

0

你需要做的是增加[self.tableView reloadData];objectsDidLoad:结束。这将使用户界面使用更新后的数据进行刷新。

相关问题