gpt4 book ai didi

ios - UITableView iOS 中的 sectionForSectionIndexTitle

转载 作者:可可西里 更新时间:2023-11-01 05:09:23 25 4
gpt4 key购买 nike

我刚开始使用 ios,我被 UITableView 困住了。

我想在 View 的右侧实现索引栏。我已经实现了这个但是当我点击索引栏上的部分时它不起作用。

这是我的代码。这里 indexArray 是“A-Z”元素,finalArray 是我的 UITableView 可变数组。

请告诉我应该在 sectionForSectionIndexTitle 中实现什么。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [finalArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return [finalArray objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return indexArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle: (NSString *)title atIndex:(NSInteger)index
{

}

最佳答案

这可能是 UITableViewDataSource 中最令人困惑的方法之一,最好考虑一个例子:

假设您有一个包含 4 个项目的 TableView :

section index - 0 section title - 'A' 
items: ['Apple', 'Ant']

section index - 1 section title - 'C'
items: ['Car', 'Cat']

然后您有 26 个部分索引标题(表格 View 右侧的索引):A - Z

现在用户点击字母“C”,您会接到电话

tableView:sectionForSectionIndexTitle:@"C" atIndex:2

您需要返回 1 - “C”部分的部分索引。

如果用户点击“Z”,您还必须返回 1,因为您只有 2 个部分,而 C 最接近 Z。

在简单的情况下,您的 TableView 中有相同的部分,因为右侧的索引有一侧可以这样做:

 - (NSInteger)tableView:(UITableView *)tableView 
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
{
return index;
}

否则取决于您的设置。您可能需要在您的章节标题中查找标题:

 NSInteger section = [_sectionTitles indexOfObject:title];
if (section == NSNotFound) // Handle missing section (e.g. return the nearest section to it?)

关于ios - UITableView iOS 中的 sectionForSectionIndexTitle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17548066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com