cocoa UITableView数据分批显示
2011-08-18 12:29
363 查看
转自: http://www.ppsim.com/blog/index.php/2011/04/449
iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 比如:
数据源是个array:
ViewController的这个方法返回数据条数: +1是为了显示\”加载更多\”的那个cell
-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {
return [self.array count]+1;
}
处理\”加载更多\”的那个cell的选择事件,触发一个方法来加载更多数据到列表
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) { /** 选择更多选项卡 */
[selfperformSelectorInBackground:@selector(loadMore) withObject:nil];
[tableViewdeselectRowAtIndexPath:indexPath animated:YES];
return;
}
}
//加载数据的方法
-(void)loadMore;
{
//获得plist文件的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@\”add\”
ofType:@\”plist\”];
//将plist文件中的数据存储到可变数组中
NSMutableArray *more = [[NSMutableArray alloc]initWithContentsOfFile:path];
[selfperformSelectorOnMainThread:@selector(appendTableWith withObject:more waitUntilDone:NO];
[more release];
}
//添加数据到列表
-(void) appendTableWith:(NSMutableArray *)data;
{
for (int i=0;i<[data count];i++) {
//将要增加的数据添加到可变数组中
[self.array addObject:[dataobjectAtIndex:i]];
}
//设置要插入行数目
NSMutableArray *insertIndexPaths =[NSMutableArray arrayWithCapacity:10];
for (int ind = 0; ind < [data count];ind++) {
//取得要添加数据所要加入cell的行号
NSIndexPath *newPath = [NSIndexPath indexPathForRow:[self.array indexOfObject:[dataobjectAtIndex:ind]] inSection:0];
[insertIndexPathsaddObject:newPath];
}
//将新加入的数据刷到已经存在的数据之后cell中
[self.tableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];
}
iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 比如:
数据源是个array:
ViewController的这个方法返回数据条数: +1是为了显示\”加载更多\”的那个cell
-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {
return [self.array count]+1;
}
处理\”加载更多\”的那个cell的选择事件,触发一个方法来加载更多数据到列表
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) { /** 选择更多选项卡 */
[selfperformSelectorInBackground:@selector(loadMore) withObject:nil];
[tableViewdeselectRowAtIndexPath:indexPath animated:YES];
return;
}
}
//加载数据的方法
-(void)loadMore;
{
//获得plist文件的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@\”add\”
ofType:@\”plist\”];
//将plist文件中的数据存储到可变数组中
NSMutableArray *more = [[NSMutableArray alloc]initWithContentsOfFile:path];
[selfperformSelectorOnMainThread:@selector(appendTableWith withObject:more waitUntilDone:NO];
[more release];
}
//添加数据到列表
-(void) appendTableWith:(NSMutableArray *)data;
{
for (int i=0;i<[data count];i++) {
//将要增加的数据添加到可变数组中
[self.array addObject:[dataobjectAtIndex:i]];
}
//设置要插入行数目
NSMutableArray *insertIndexPaths =[NSMutableArray arrayWithCapacity:10];
for (int ind = 0; ind < [data count];ind++) {
//取得要添加数据所要加入cell的行号
NSIndexPath *newPath = [NSIndexPath indexPathForRow:[self.array indexOfObject:[dataobjectAtIndex:ind]] inSection:0];
[insertIndexPathsaddObject:newPath];
}
//将新加入的数据刷到已经存在的数据之后cell中
[self.tableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];
}
相关文章推荐
- 【學習筆記】tableview分批显示数据
- UITableView(二)----显示数据
- UITableView 查询结果为空,显示无数据的方法
- ListView,GridView,Spinner等 无数据时显示背景view
- GridView模版列嵌套GirdView显示主从表数据
- 提取Chrome的JSONView扩展以便在页面上显示美化了的JSON数据
- 让浏览器格式化显示JSON数据之chrome jsonView插件安装
- IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)
- ios tableview 那些事(二十五)用故事板 自定义Tableview 不显示数据注意事项。
- IOS 构造和使用TableView(基于storyboard) (一)基本的数据显示
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- viewpager嵌套fragment,fragment放置后台时间过长,不显示数据
- RecyclerView 显示不同结构数据
- 获取json数据显示RecyclerView
- Android ListView数据的分批显示功能
- GridView,FormView控制日期数据的显示格式
- Android下拉刷新库,利用viewdraghelper实现,集成了下拉刷新,底部加载更多,数据初始加载显示loading等功能
- tableView视图列表3: 把解析到的XML数据显示在TableView上
- 解决:RecyclerView只显示一行数据
- 关于RecyclerView只显示一行数据的另类解决办法