参考:http://my.oschina.net/joanfen/blog/204503
思路:UITableView一次性加载数据过多时,需要滑动多次触底。想通过索引实现快速滑动,索引中加载20个空点。用户在最右端滑动时,索引框显示,当触及索引点时指向其想对应的UITableView的RowIndex来实现快速滚动。这方法有缺陷:普通滑动时滚动条被遮盖了。
主要代码:
//获取数据
-(void)getTableData{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
// 获取数据库数据
self.listArray = [[ReportLogic sharedInstance] getProductByCategory];
if ([self.listArray count] == 0) {
[GlobalApplication Alert:@"提示" :@"暂无数据"];
}else{
// 索引目录,20个空点
NSMutableArray *stArray = [[NSMutableArray alloc] init];
self.sectionTitles = stArray;
[stArray release];
for (int i=0;i<20;i++)
{
NSString *index = @"";
[self.sectionTitles insertObject:index atIndex:i];
}
}
// 数据刷新
[self.fmTableView reloadData];
});
});
}
#pragma mark index
// 分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
// 索引目录
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.sectionTitles;
}
// 滑动时点击目录
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
// 修正索引焦点为UITableView的RowIndex,头尾和中间值
if (index == 0) {
index = 1;
}else if(index == self.sectionTitles.count - 1){
index = self.listArray.count -1;
}else
index = round(index*self.listArray.count/20);
[self.fmTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
return index;
}
效果: