だから、私は奇妙な問題を抱えています。私は Parse の iOS API を使用しており、PFQueryTableViewController を使用してアイテムをユーザーに表示しています。これらの項目は 2 つのセクションに分かれています。アプリケーションが最初に起動すると、objectsDidLoad
メソッドを実行する前にメソッドが実行されnumberOfRowsInSection
ます。これは完璧ですobjectsDidLoad
メソッドは、テーブル ビューの各セクションに存在するオブジェクトの数を定義できます。また、完全に機能しているtableViewの「プルダウンして更新」機能を有効にしています。そのため、解析データ ブラウザで特定のデータ値を変更すると、ユーザーに表示されるアイテムの 1 つがセクションを変更するはずです。そのデータ値を変更した後にアプリを再起動すると、オブジェクトは他のセクションに表示されます。ただし、問題は、解析時にデータ値を変更してオブジェクトのセクションを変更し、tableView をプルダウンしてデータを更新すると、アプリがクラッシュし、その理由はわかっていると思います。このメソッドは、アプリの初回objectsDidLoad
実行時にメソッドの前に実行されますが、実際にはアプリの実行後に実行されます。numberOfRowsInSection
numberOfRowsInSection
tableView をプルダウンしてデータを更新すると。これにより、cellForRowAtIndexPath
メソッドは、各セクションにオブジェクトを格納する配列に存在しない配列インデックスにアクセスしようとします。
numberOfRowsInSection
私の質問:メソッドが呼び出される前に、オブジェクトを更新し、各セクションのオブジェクトを格納する配列を更新するにはどうすればよいですか?
これが私のobjectsDidLoad
方法です:
- (void)objectsDidLoad:(NSError *)error {
//dataArray, firstSectionArray, and secondSectionArray are global NSMutableArrays
[super objectsDidLoad:error];
firstSectionArray = [[NSMutableArray alloc]init];
secondSectionArray = [[NSMutableArray alloc]init];
NSMutableDictionary *firstSectionDictionary = [[NSMutableDictionary alloc]init];
NSMutableDictionary *secondSectionDictionary = [[NSMutableDictionary alloc]init];
dataArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [self.objects count]; i++) {
if ([[[self.objects objectAtIndex:i]objectForKey@"Level"]intValue] == 1) {
[firstSectionArray addObject:[self.objects objectAtIndex:i]];
}
else {
[secondSectionArray addObject:[self.objects objectAtIndex:i]];
}
}
firstSectionDictionary = [NSMutableDictionary dictionaryWithObject:firstSectionArray forKey:@"data"];
secondSectionDictionary = [NSMutableDictionary dictionaryWithObject:secondSectionArray forKey:@"data"];
[dataArray addObject:firstSectionDictionary];
[dataArray addObject:secondSectionDictionary];
}
これが私のnumberOfRowsInSection
方法です:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dictionary = [dataArray objectAtIndex:section];
NSMutableArray *array = [dictionary objectForKey:@"data"];
return [array count];
}
これが私のcellForRowAtIndexPath
方法です:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSMutableDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
//This next line is where it's crashing
PFObject *newObject = [[dictionary objectForKey:@"data"]objectAtIndex:indexPath.row];
//More irrelevant code below..........
}