我有一個分組的uitableview有幾個部分。這些部分顯示正常,行數正確,但列表項的位置有問題。我有一個名字列表的NSMutableArray,然後我使用subArrayWithRange分割這個數組。請參閱代碼以獲取更多詳細信息。問題是列表項目沒有出現在正確的部分,我似乎無法找到解決方案。iPhone SDK:分組的UITableView部分,列表項不顯示在正確的部分
在的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;
下面的答案能幫助你嗎?你是否仍然有這個問題需要解決? – 2012-01-14 12:04:28