我有一个分组的 uitableview,它有几个部分。这些部分显示正常并且行数正确,但是列表项的定位存在问题。我有一个带有名称列表的 NSMutableArray,然后我使用 subArrayWithRange 将该数组拆分。请参阅代码以获取更多详细信息。问题是列表项没有出现在正确的部分下,我似乎找不到解决方案。
在 cellForRowAtIndexPath 中:
NSInteger section = [indexPath section];
NSString *sectionName = [sectionNamesArray objectAtIndex:section];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:townArray];
int numOfRows = [set countForObject:[NSString stringWithFormat:@"%@",sectionName]];
int initNumRows = [set countForObject:[NSString stringWithFormat:@"%@",[sectionNamesArray objectAtIndex:0]]];
if(section == 0) {
NSRange range = NSMakeRange(0,numOfRows);
NSArray *subArray = [[namesArray subarrayWithRange:range] retain];
NSArray *subTownArray = [[townArray subarrayWithRange:range]retain];
//indexTracker = indexPath.row;
//NSLog(@"sub array === %@, index Tracker = %i",subArray,indexTracker);
cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row];
cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row];
} else if (section == [sectionNamesArray count]-1) {
int previousSection = section - 1;
int previousNumRows = [set countForObject:[NSString stringWithFormat:[sectionNamesArray objectAtIndex:previousSection]]];
NSRange range = NSMakeRange(previousNumRows+initNumRows,numOfRows);
NSLog(@"previous num rows = %i and current num of rows = %i",previousNumRows,numOfRows);
NSArray *subArray = [[namesArray subarrayWithRange:range] retain];
NSArray *subTownArray = [[townArray subarrayWithRange:range]retain];
cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row];
cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row];
NSLog(@"sub array === %@",subArray);
} else {
int previousSection = section - 1;
int previousNumRows = [set countForObject:[NSString stringWithFormat:[sectionNamesArray objectAtIndex:previousSection]]];
int location = previousNumRows;
NSRange range = NSMakeRange(previousNumRows,numOfRows);
NSLog(@"location = %i",location);
NSLog(@"previous num rows = %i and current num of rows = %i",previousNumRows,numOfRows);
NSArray *subArray = [[namesArray subarrayWithRange:range] retain];
NSArray *subTownArray = [[townArray subarrayWithRange:range]retain];
cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row];
cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row];
} //NSLog(@"section name = %@ . num of rows = %i",sectionName,numOfRows);
return cell;