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] という行にコメントを付けると、冗長なセルが表示されます。