详解iPhone Tableview分批显示数据

iPhone Tableview分批显示数据是本文要介绍的内容,主要讲解的是数据的显示。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];  
}  
小结:详解iPhone Tableview分批显示数据的内容介绍完了,希望通过本文的学习能对你有所帮助!
 
 
posted @   郑文亮  阅读(374)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2012-01-05 iPhone的解锁、越狱、激活、固件等等是什么意思,有什么分别?(转)
2012-01-05 iOS Programming – 触摸事件处理 (转)
2012-01-05 英语单词学习
2012-01-05 objective-c与java的几点不同 (转)
2012-01-05 慎用ViewController的跳转 (转)
2012-01-05 NSArray,NSSet,NSDictionary总结 (转)
点击右上角即可分享
微信分享提示