use*_*198 3 objective-c uitableview ios
我想知道是否可以提供帮助,即使您在表格视图中只有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;
}
Run Code Online (Sandbox Code Playgroud)
不返回索引。仅返回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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2958 次 |
| 最近记录: |