数据分批显示

iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 
比如: 

数据源是个array:

ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.array count]+1;

}


处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == [items count]) {
        [self performSelectorInBackground:@selector(loadMore) withObject:nil];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
}


//加载数据的方法


-(void)loadMore;

{

//获得plist文件的路径

    NSString *path = [[NSBundle mainBundlepathForResource:@"add" 

 ofType:@"plist"];

//plist文件中的数据存储到可变数组中

NSMutableArray *more = [[NSMutableArray allocinitWithContentsOfFile:path];

[self performSelectorOnMainThread:@selector(appendTableWith:) withObject:morewaitUntilDone:NO];

[more release];

}


//添加数据到列表


-(void) appendTableWith:(NSMutableArray *)data;

{

for (int i=0;i<[data count];i++) {

//将要增加的数据添加到可变数组中

        [self.array addObject:[data objectAtIndex: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];

        [insertIndexPaths addObject:newPath];

    }

//将新加入的数据刷到已经存在的数据之后cell

    [self.tableView insertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];

}

本文内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值