iPhone SDK:分组的UITableView部分,列表项未在正确的部分下显示

时间:2012-01-10 11:08:35

标签: iphone uitableview nsrange

我有一个分组的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;

1 个答案:

答案 0 :(得分:1)

我认为你过于复杂。为什么不用您拥有的数据创建您的部分(仅包含部分标题的词典),然后在cellForRowAtIndexPath中执行类似的操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *selRow = [[self.units valueForKey:[[[self.units allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

...
}

以下是我用于构建Dictionary的方法:

-(void)buildSectionKeysWithMutableArray:(NSMutableArray *)mutableArray {
    NSMutableString *prevUnit;
    units = [[NSMutableDictionary alloc] init];

    // Loop through the mutableArray passed in and build our keys
    for (NSDictionary *thisUnit in mutableArray)
    {
        NSString *unitName = [thisUnit objectForKey:@"LOCATION"];
        if (![prevUnit isEqualToString:unitName]) {
            [self.units setValue:[[NSMutableArray alloc] init] forKey:unitName];                
        }
        prevUnit = [NSString stringWithString:unitName];
    }

    // Loop again and add the keys into the units mutableDictionary
    for (NSDictionary *thisUnit in mutableArray)
    {
        [[self.units objectForKey:[thisUnit objectForKey:@"LOCATION"]] addObject:thisUnit];
    }

}

这使您可以保持数据的呈现动态。