米国のすべての州がアルファベット順のセクションにリストされたテーブル ビュー コントローラーがあります。状態をクリックすると、Web サービス呼び出しを介して状態に関する詳細情報が返されます。これは、セクション化またはグループ化されたテーブル ビューでの私の最初の試みであり、'A' を過ぎた行のインデックス パスに問題があります。
たとえば、「C」グループの最初のアイテムである「カリフォルニア」をクリックすると、indexpath.row プロパティは 4 ではなく 0 になります。CA はアルファベット順で 5 番目の州であるため、indexpath があるはずです。 .4 の行。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//get the letter in the current section
NSString *letter = [stateIndex objectAtIndex:[indexPath section]];
//get all the states beginning with the letter
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", letter];
NSArray *states = [stateKeys filteredArrayUsingPredicate:predicate];
if([states count] > 0){
//get relevant state from states object
NSString *cellValue = [states objectAtIndex:indexPath.row];
[[cell textLabel] setText:cellValue];
}
return cell;
}
私の続編では、このメソッドの最後の行にブレークポイントを設定すると、itemRowIndex が正しくないことがわかります (もちろん、'A' グループを除く):
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"sgShowStateRivers"]){
RiversByStateTableViewController *riversVC = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger itemRowIndex = indexPath.row;
NSString *key = [stateKeys objectAtIndex:itemRowIndex];
[riversVC setStateIdentifier:[statesDict objectForKey:key]];
}
}
元の配列と同じようにアイテムにまだ番号が付けられているグループ化されたテーブル ビューを作成するにはどうすればよいでしょうか。ありがとう!Ⅴ