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

发布于 2024-12-26 02:13:26 字数 2690 浏览 0 评论 0原文

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

I have a grouped uitableview which has several sections. The sections are showing up fine and the number of rows is correct, however there is a problem with the positioning of the list items. I have an NSMutableArray with a list of names, I then split this array up using subArrayWithRange. Please see the code for more detail. The problem is that the list items are not appearing under the correct section, I can't seem to find the solution.

In 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2025-01-02 02:13:26

我认为你把事情过于复杂化了。为什么不使用您拥有的数据创建您的部分(仅包含部分标题的字典),然后在 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];

...
}

这是我用于构建该字典的方法:

-(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];
    }

}

这允许您保留数据的表示形式动态的。

I think you are over-complicating things. Why not create your sections (a Dictionary with just the section titles) with the data you have and then do something like this in the 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];

...
}

Here is the method I use for building that 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];
    }

}

This allows you to keep the presentation of the data dynamic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文