Ios 分区表视图索引问题
我有一个表视图控制器,所有美国州都按字母顺序列出。单击状态将通过web服务调用返回有关该状态的详细信息。这是我第一次尝试使用分区或分组表视图,但对于超过“a”的行的索引路径,我遇到了问题 例如,如果我单击“C”组中的第一项“California”,indexpath.row属性将为0而不是4,因为CA是按字母顺序排列的第五个州,所以它的indexpath.row属性应为4Ios 分区表视图索引问题,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个表视图控制器,所有美国州都按字母顺序列出。单击状态将通过web服务调用返回有关该状态的详细信息。这是我第一次尝试使用分区或分组表视图,但对于超过“a”的行的索引路径,我遇到了问题 例如,如果我单击“C”组中的第一项“California”,indexpath.row属性将为0而不是4,因为CA是按字母顺序排列的第五个州,所以它的indexpath.row属性应为4 - (UITableViewCell *)tableView:(UITableView *)tableView cellFo
- (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”组除外):
如果项目仍然按照原始数组中的项目编号,那么如何拥有分组表视图?谢谢V您在
UITableView
上下文中对nsindepath
s的理解是不正确的。对于表中的每个部分,行
属性重置为0
您有两个简单的前进选项:
- 您可以做的是创建一个二维数组(“数组数组”),其中第一级用于每个部分,第二级用于每个部分中的每行。这使您可以直接使用索引路径的
和行
进行数组查找节
- 在
中,您可以循环各节并计算行数,直到到达prepareForSegue:sender:
为止,然后将indexPathForSelectedRow.section
添加到该计数中。这将为您提供一个索引,您可以使用该索引从一维数组中获取状态信息indexPathForSelectedRow.row
UITableView
上下文中对nsindepath
s的理解是不正确的。对于表中的每个部分,行
属性重置为0
您有两个简单的前进选项:
- 您可以做的是创建一个二维数组(“数组数组”),其中第一级用于每个部分,第二级用于每个部分中的每行。这使您可以直接使用索引路径的
和行
进行数组查找节
- 在
中,您可以循环各节并计算行数,直到到达prepareForSegue:sender:
为止,然后将indexPathForSelectedRow.section
添加到该计数中。这将为您提供一个索引,您可以使用该索引从一维数组中获取状态信息indexPathForSelectedRow.row
-(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]];
}
}