gpt4 book ai didi

objective-c - 具有 1 个部分的 UITableview 索引

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:53 27 4
gpt4 key购买 nike

我想知道您能否帮助我,我读到过可以设置索引滚动,即使您的 TableView 中只有 1 个部分。我有很多排序的表条目(超过 50 个),并希望跳转到 A、B、C、D 等开头的行,如下所示 - 我对此很陌生,我所拥有的很接近,但它无法正常工作 -你能帮我吗 ?

我的代码如下:-

enter code here 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:@"A"];
[tempArray addObject:@"B"];
[tempArray addObject:@"C"];
[tempArray addObject:@"D"];
[tempArray addObject:@"F"];
[tempArray addObject:@"G"];
[tempArray addObject:@"H"];
[tempArray addObject:@"J"];
[tempArray addObject:@"K"];
[tempArray addObject:@"L"];
[tempArray addObject:@"M"];
[tempArray addObject:@"P"];
[tempArray addObject:@"S"];
[tempArray addObject:@"T"];
[tempArray addObject:@"U"];
[tempArray addObject:@"V"];
return tempArray;
}

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

if(index !=0){
[tableView scrollToRowAtIndexPath:[NSIndexPath
indexPathForRow:index inSection:0]
atScrollPosition:UITableViewScrollPositionTop animated:TRUE];
}
return index;
}

最佳答案

不返回索引。只返回 1,这不是一个存在的节索引。另外,需要去掉if判断。这使得该部分无法滚动,然后您可以手动滚动到您自己的单元格。我认为您需要找到与所选字母表的第一个匹配项,而不仅仅是使用索引来定位单元格。

回答下面的评论:它在我的项目中完美运行。下面是我的流程,希望对你有帮助:假设您在该表中只有一个部分。

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
// Find the correct index of cell should scroll to.
int foundIndex = 0;
for (Object *obj in dataArray) {
if ([[[obj.YOURNAME substringToIndex:1] uppercaseString] compare:title] == NSOrderedSame || [[[obj.YOURNAME substringToIndex:1] uppercaseString] compare: title] == NSOrderedDescending)
break;
foundIndex++;
}
if(foundIndex >= [dataArray count])
foundIndex = [dataArray count]-1;

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:foundIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

return 1;
}

关于objective-c - 具有 1 个部分的 UITableview 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9916762/

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