我有一个应用程序,它从3个共享的Google日历中获取事件并将其显示在表格视图中。拉动以刷新崩溃的应用程序
我想实现拉刷新,但应用程序不断崩溃,如果我放开拉,在数据加载之前。 (如果我憋了几秒钟拉一切都很好 - 如果我松手马上崩溃
代码:
-(void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(getEvents) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
startDates = [[NSMutableArray alloc] init];
[self getEvents];
}
- (void)stopRefresh
{
[self.refreshControl endRefreshing];
}
-(void)getEvents
{
[startDates removeAllObjects];
startDates = [NSMutableArray array];
sectionEntries = [NSMutableArray array];
entries = [NSMutableArray array];
sortedStartDates = [[NSArray alloc]init];
_imageForCalendarType = [[NSDictionary alloc]init];
_imageForCalendarType = @{
@"The Irish House Music Calendar" : [UIImage imageNamed:@"music.png"]
, @"FixedEvents-Student Night" : [UIImage imageNamed:@"student.png"]
, @"FixedEvents-Ladies Night" : [UIImage imageNamed:@"cocktail.png"]
, @"AppTest" : [UIImage imageNamed:@"football.png"]
};
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:sportsCalendarURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
NSData* data2 = [NSData dataWithContentsOfURL:musicCalendarURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data2 waitUntilDone:YES];
NSData* data3 = [NSData dataWithContentsOfURL:fixedCalendarURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data3 waitUntilDone:YES];
// Reload table view - UI operation, so must be run on main thread
dispatch_async(dispatch_get_main_queue(), ^{
sortedStartDates = [startDates sortedArrayUsingSelector:@selector(compare:)];
[self.tableView reloadData];
[self performSelector:@selector(stopRefresh) withObject:nil afterDelay:2.5];
});
});
}
它给了我在这一行cellForRowAtIndexPath方法中一个SIGABRT错误。
NSInteger index = [self getRow:sortedStartDates[indexPath.section]]; // get correct index for sectionEntries
错误:*终止应用程序由于未捕获的异常 'NSRangeException',原因: '* - [__ NSArrayI objectAtIndex:]:索引4超越界限为空数组'
这似乎是错误是因为我的startDates没有数据NSMutableArray,但如果我评论行[startDates removeAllObjects]我得到多余的单元格。
无论发生什么问题:冗余或额外的单元格,tableview数据源配置不正确。首先检查'numberOfRowsInSection'和'numberOfSectionsInTableView'方法的结果。 –
只有在实际更新数据之前放开“刷新”才会出现此问题。在我看来,应用程序试图在我有任何数据放入它们之前创建单元格 – catu