【问题标题】:iOS Section Index scrolls to wrong positioniOS部分索引滚动到错误的位置
【发布时间】:2014-01-12 10:47:47
【问题描述】:

我有一个非常标准的 UITableView,带有部分(A-Z - 没有数字或搜索)。

我已经实现了在表格右侧启用节索引(A-Z 栏)视图的委托方法。

我现在已经在表格中输入了足够的数据,使得大多数(不是全部,而是大多数)部分(即 A、B、C...Z)都有一个或多个单元格。

但是我注意到,现在该应用程序处于私人测试阶段,当有人触摸部分索引的字母时,表格跳转到的实际位置是不正确的。

它实际上对 A、B 和 C 是正确的,然后逐渐变得越来越错误 - 以至于 Z 实际上是条上“R”所在的位置。

这是在 iPad 上(仅限),并且没有对表格或部分索引视图进行特殊的“幕后”定制。

有什么想法吗?我已经在谷歌上搜索了将近一个小时,但找不到以前见过这个的人。

编辑:

这是我设置部分索引的代码:

- ( NSArray * ) sectionIndexTitlesForTableView:( UITableView * )tableView
{
    [self customizeSectionIndexView]; // This just changes colours in iOS 7+

    return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
}



- ( NSString * ) tableView:( UITableView * )tableView titleForHeaderInSection:( NSInteger )section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsControllerWithPredicate sections] objectAtIndex:section];

    return [sectionInfo name];
}



- ( NSInteger ) numberOfSectionsInTableView:( UITableView * )tableView
{
    NSInteger sectionCount = [[self.fetchedResultsControllerWithPredicate sections] count];

    return sectionCount;
}



- ( NSInteger ) tableView:( UITableView * )tableView numberOfRowsInSection:( NSInteger )section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsControllerWithPredicate sections][section];

    return [sectionInfo numberOfObjects];
}

所以我最多有 26 个部分,但我只是想显示其中包含单元格的部分。这是我要出错的地方吗?

当数据来自 Core Data 并且可以随时更改时,我该如何处理这种状态?

【问题讨论】:

    标签: ios uitableview scroll indexing


    【解决方案1】:

    您需要确保拥有与索引一样多的部分,例如,如果您正在执行 A..Z,则需要 26 个索引(“A”、“B”、“C”等)以及 26 个表格部分。

    如果您也将表格部分标题显示为字母,并且如果该部分中没有单元格,您不希望部分标题出现,则可以为该部分返回 nil。

    这就是我的做法:

    -(NSArray*) letters {
    
        return @[@"#", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    
    }
    
    -(NSArray*) sectionIndexTitlesForTableView:(UITableView*)tableView {
        return [self letters];
    }
    
    -(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView {
        return [self letters].count;
    }
    
    -(NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
    
        // Get array of items in this section
        NSArray* sectionItems = [allSections objectAtIndex:section];
    
        // Return letter, or nil if no items in this section
        return (sectionItems.count > 0 ? [[self letters] objectAtIndex:section] : nil);
    
    }
    

    【讨论】:

    • 您好 JJV360。谢谢。这与我的代码几乎相同;除了我有 26 个部分(A-Z),但根据输入核心数据并由表格显示的数据,表格中的“实际”部分可能较少。这是问题吗?如果是这样,我该如何解决?
    • 是的,这就是问题所在,你必须有 26 个表部分,即使其中一些没有行
    • 太棒了!你已经指出了它。但是,当数据来自 Core Data 并且单元格可能没有 26 种不同类型的数据(即每个字母对应一个)时,我该怎么做?我错过了一些明显的东西吗?大声笑
    • 我对 Core Data 了解不多,但如果你不能让它有 26 个部分,那么如果你更改 sectionIndexTitlesForTableView 中返回的数组并只返回有表格部分(即“A”、“B”、“D”、“G”等......)
    • 好的,我会试一试...然后告诉你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2018-08-02
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多