UITableView iOS中SectionIndexTitle的部分
我刚开始使用ios,我一直在使用UITableView iOS中SectionIndexTitle的部分,ios,tableview,Ios,Tableview,我刚开始使用ios,我一直在使用UITableView 我想在视图的右侧实现索引栏。我已经实现了这个功能,但是当我点击索引栏上的部分时,它就不起作用了 这是我的密码。这里的indexArray是“A-Z”元素,finalArray是我的UITableView可变数组 请告诉我应该在sectionIndexTitle的部分中实现什么 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [fin
UITableView
我想在视图的右侧实现索引栏。我已经实现了这个功能,但是当我点击索引栏上的部分时,它就不起作用了
这是我的密码。这里的indexArray
是“A-Z”元素,finalArray
是我的UITableView
可变数组
请告诉我应该在sectionIndexTitle的部分中实现什么
- (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
{
}
这可能是<代码> UITababVIEW DATABOSURCE 中最令人困惑的方法之一,最好考虑一个例子:
假设您有一个包含4项的表视图:
section index - 0 section title - 'A'
items: ['Apple', 'Ant']
section index - 1 section title - 'C'
items: ['Car', 'Cat']
然后有26个部分索引标题(表视图右侧的索引):A-Z
现在用户点击字母“C”,你就会接到一个电话
tableView:sectionForSectionIndexTitle:@"C" atIndex:2
您需要返回1-C节的节索引
如果用户点击“Z”,你也必须返回1,因为你只有2个部分,C是最接近Z的
在简单的情况下,如果表视图中有与右侧索引相同的部分,则可以这样做:
- (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?)
因为你没有归还任何东西。阅读以下内容:如果不需要,请删除该方法。你应该准备好了。谢谢,但如果我不使用sectionforsectiontitle。点击索引栏,一切都不会发生。这就是为什么我需要实现这个。那么我应该返回什么呢?谢谢,我遇到了同样的问题,我按照您的建议返回索引,表视图只显示可用部分,但索引显示从A到Z的所有字符。更改后,当点击其他字符时,它将使用最接近的部分。