转:详解iPhone Tableview分批显示数据 点击加载更多

iPhone Tableview分批显示数据是本文要介绍的内容,主要讲解的是数据的显示。iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 比如:
数据源是个array:
1.        NSMutableArray *items; 
ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell
1.        - (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {  
2.                int count = [items count];  
3.            return  count + 1;  
4.        } 
这个方法定制cell的显示, 尤其是"加载更多"的那个cell:
1.        - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {  
2.            if([indexPath row] == ([items count])) {  
3.                //创建loadMoreCell  
4.                return loadMoreCell;  
5.            }  
6.            //create your data cell  
7.            return cell;  
8.        } 
还要处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表
1.        - (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {  
2.              
3.            if (indexPath.row == [items count]) {  
4.                [loadMoreCell setDisplayText"loading more ..."];  
5.                [loadMoreCell setAnimating:YES];  
6.                [self performSelectorInBackgroundselector(loadMore) withObject:nil];  
7.                //[loadMoreCell setHighlighted:NO];  
8.                [tableView deselectRowAtIndexPath:indexPath animated:YES];  
9.                return;  
10.            }  
11.            //其他cell的事件  
12.        } 
加载数据的方法:
1.        -(void)loadMore  
2.        {  
3.            NSMutableArray *more;   
4.                //加载你的数据  
5.            [self performSelectorOnMainThreadselector(appendTableWith withObject:more waitUntilDone:NO];  
6.        } 
添加数据到列表:
1.        -(void) appendTableWithNSMutableArray *)data  
2.        {  
3.            for (int i=0;i<[data count];i++) {  
4.                [items addObject:[data objectAtIndex:i]];  
5.            }  
6.            NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];  
7.            for (int ind = 0; ind < [data count]; ind++) {  
8.

参考:http://blog.163.com/cai_1111/blog/static/198815027201110905933871/

http://www.oschina.net/question/916968_88945

Debug:

1、再使用点击加载更多时,一定要注意,UITableView 要在viewDidLoad方法中初始化,否则每点一次“点击加载更多”tableview就初始化一次。数据没法从你点击的位置开始显示。

原文地址:https://www.cnblogs.com/ygm900/p/3101417.html