【學習筆記】tableview分批显示数据
2011-11-01 13:12
337 查看
iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察 看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时,
添加更多的数据到数据源中. 比如:
数据源是个array:
NSMutableArray *items;
ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int count = [items count];
return count + 1;
}
这个方法定制cell的显示, 尤其是"加载更多"的那个cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == ([items count])) {
//创建loadMoreCell
return loadMoreCell;
}
//create your data cell
return cell;
}
还要处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) {
[loadMoreCell setDisplayText:@"loading more ..."];
[loadMoreCell setAnimating:YES];
[self performSelectorInBackground:@selector(loadMore) withObject:nil];
//[loadMoreCell setHighlighted:NO];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
//其他cell的事件
}
加载数据的方法:
-(void)loadMore
{
NSMutableArray *more;
//加载你的数据
[self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
}
添加数据到列表:
-(void) appendTableWith:(NSMutableArray *)data
{
for (int i=0;i<[data count];i++) {
[items addObject:[data objectAtIndex:i]];
}
NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];
for (int ind = 0; ind < [data count]; ind++) {
NSIndexPath *newPath = [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];
[insertIndexPaths addObject:newPath];
}
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
原文地址:http://hi.baidu.com/popln/blog/item/6729a63c186dc9d69f3d62b1.html
添加更多的数据到数据源中. 比如:
数据源是个array:
NSMutableArray *items;
ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int count = [items count];
return count + 1;
}
这个方法定制cell的显示, 尤其是"加载更多"的那个cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == ([items count])) {
//创建loadMoreCell
return loadMoreCell;
}
//create your data cell
return cell;
}
还要处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) {
[loadMoreCell setDisplayText:@"loading more ..."];
[loadMoreCell setAnimating:YES];
[self performSelectorInBackground:@selector(loadMore) withObject:nil];
//[loadMoreCell setHighlighted:NO];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
//其他cell的事件
}
加载数据的方法:
-(void)loadMore
{
NSMutableArray *more;
//加载你的数据
[self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
}
添加数据到列表:
-(void) appendTableWith:(NSMutableArray *)data
{
for (int i=0;i<[data count];i++) {
[items addObject:[data objectAtIndex:i]];
}
NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];
for (int ind = 0; ind < [data count]; ind++) {
NSIndexPath *newPath = [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];
[insertIndexPaths addObject:newPath];
}
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
原文地址:http://hi.baidu.com/popln/blog/item/6729a63c186dc9d69f3d62b1.html
相关文章推荐
- IOS tableView数据显示
- Tableview 更多数据的显示方法
- (iOS开发)当tableView数据较少的时候,隐藏下面,并显示白色
- TableView 根据数据显示分割线
- QTableview QSqlTableModel如何最恰当地只显示一个表中的某几个字段的数据
- QT查询数据库中的数据在tableview显示
- iOS 类似股票数据显示的可上下左右滚动TableView
- tableView视图列表3: 把解析到的XML数据显示在TableView上
- DZNEmptyDataSet 当没有数据时tableview,collection的显示
- IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)
- NSXMLParser读取XML文件并将数据显示到TableView上
- 当数据量很少的时候,tableview会显示多余的cell--iOS开发系列---项目中成长的知识二
- QT查询数据库中的数据在tableview显示
- iOS 不同按钮点击显示不同的tableview数据
- tableView 和聊天对话一样,显示最新的数据,不用滑动
- 用json方法解析本地数据,并显示在tableView上面
- 解决tableView无数据时还显示横线及线条显示不全问题
- cocoa UITableView数据分批显示
- iOS开发中关于没有数据时不显示tableView的分割线
- IOS 构造和使用TableView(基于storyboard) (一)基本的数据显示