数据分批显示

来源:互联网 发布:下载驱动人生软件 编辑:程序博客网 时间:2024/05/12 22:00

 

Filed under: 趣味 —  admin @ 11:34 上午
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 = [[NSBundlemainBundle]pathForResource:@\”add\”

ofType:@\”plist\”];

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

NSMutableArray *more = [[NSMutableArrayalloc]initWithContentsOfFile:path];

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

[morerelease];

}

//添加数据到列表

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

{

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

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

        [self.arrayaddObject:[dataobjectAtIndex:i]];

    }

//设置要插入行数目

   NSMutableArray *insertIndexPaths = [NSMutableArrayarrayWithCapacity:10];

   for (int ind = 0; ind < [datacount]; ind++) {

//取得要添加数据所要加入cell的行号

       NSIndexPath    *newPath =  [NSIndexPathindexPathForRow:[self.arrayindexOfObject:[dataobjectAtIndex:ind]]inSection:0];

        [insertIndexPathsaddObject:newPath];

    }

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

    [self.tableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];

}