`
hunxiejun
  • 浏览: 1148995 次
文章分类
社区版块
存档分类
最新评论

cocoa UITableView数据分批显示

 
阅读更多

转自:http://www.ppsim.com/blog/index.php/2011/04/449

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

数据源是个array:

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

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

return [self.array count]+1;

}


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

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == [items count]) { /** 选择更多选项卡 */

[selfperformSelectorInBackground:@selector(loadMore) withObject:nil];

[tableViewdeselectRowAtIndexPath:indexPath animated:YES];

return;

}

}

//加载数据的方法

-(void)loadMore;

{

//获得plist文件的路径

NSString *path = [[NSBundle mainBundle]pathForResource:@\”add\”

ofType:@\”plist\”];

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

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

[selfperformSelectorOnMainThread:@selector(appendTableWith withObject:more waitUntilDone:NO];

[more release];

}

//添加数据到列表

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

{

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

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

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

[insertIndexPathsaddObject:newPath];

}

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

[self.tableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];

}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics