【问题标题】:Indexed UITableView, like Contacts.app索引 UITableView,如 Contacts.app
【发布时间】:2013-06-15 12:15:36
【问题描述】:

我想复制 Contacts.app 的滚动条行为。

我知道你必须实现这两种方法:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;

这就是我所做的:第一个返回 A 到 Z 的字符数组,第二个返回索引。 到目前为止,它工作得很好:我得到了滚动条,滚动它会使 UITableView 滚动。

但我有两个问题:

首先,问题是,如果我没有字母 C 的联系人,那么滚动显然会交错,滚动 C 将显示 D 索引,滚动 D 将显示 E 索引。 .. 我虽然关于搜索最近的部分,但这是最有效的方法吗? 类似于在我的课堂上实现这个功能:

- (NSInteger) getSectionIndexForSectionTitle:(NSString *section) {
    if ([_allSections objectForKey:section] == nil) {
        NSComparisonResult res = NSOrderedAscending;
        for (NSString* sectionTitle in _allSections) {
            NSComparisonResult tmp = [sectionTitle compare:section]; 
            if (tmp == NSOrderedDescending && res == NSOrderedAscending)
                return [(NSNumber *)[_allSections objectForKey:sectionTitle] intValue];
            res = tmp;
        }
        return [_allSections count] - 1;
    }
    return [(NSNumber *)[_allSections objectForKey:section] intValue];
}

但我担心这种解决方案可能会提供糟糕的性能。

第二个问题,可能与第一个问题有关:在 Contacts.app 中,您看不到滚动条中的“O”字母,但您仍然可以滚动到此部分。 关于如何实现这一目标的任何想法?

感谢阅读和帮助我!

【问题讨论】:

    标签: ios objective-c uitableview indexed


    【解决方案1】:

    您无需修改​​节标题高度。有一个专为您正在寻找的目的而设计的类:UILocalizedIndexedCollat​​ion。 using it to get index scrolling in a table view 上有一篇很棒的文章。

    【讨论】:

    • 看起来很有趣;会看看它!感谢您的链接。
    【解决方案2】:

    为了解决您的问题,我认为有一个替代解决方案.. 尝试更改 heightForHeaderInSection 以隐藏部分标题,这会使空白部分被隐藏,因此当滚动到该部分索引时,它将显示最接近的部分。

    您将所有部分标题保留在从 sectionIndexTitlesForTableView 返回的数组中,然后用户可以滚动到该位置。

    所以实现 heightForHeaderInSection 以隐藏没有行的部分,并且在您的 sectionIndexTitlesForTableView 中,sectionForSectionIndexTitle 将只返回您拥有的任何内容并且不要尝试操作索引。

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
    {
        NSInteger nRowCount = [[_arrSectionCounts objectAtIndex:section] integerValue];
        if ( nRowCount == 0 ) {
            return 0.0f;
        }
    
        return 22.0f;
    }
    

    【讨论】:

    • 感谢您的回答。显然,这看起来更有效率。但我看不出它如何解决另一个问题(滚动条中没有“O”字母)?我试图在- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 中添加一个带有空字符串的部分,但如果这样做,我将在滚动条中有一个空白区域,这是我不想要的。
    猜你喜欢
    • 2013-06-27
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多