詳解iPhone Tableview分批顯示數(shù)據(jù)
iPhone Tableview分批顯示數(shù)據(jù)是本文要介紹的內(nèi)容,主要講解的是數(shù)據(jù)的顯示。iPhone屏幕尺寸是有限的,如果需要顯示的數(shù)據(jù)很多,可以先數(shù)據(jù)放到一個(gè)table中,先顯示10條,table底部有一察看更多選項(xiàng),點(diǎn)擊察看更多查看解析的剩余數(shù)據(jù)?;旧暇褪?strong>數(shù)據(jù)源里先只放10條, 點(diǎn)擊***一個(gè)cell時(shí), 添加更多的數(shù)據(jù)到數(shù)據(jù)源中. 比如:
數(shù)據(jù)源是個(gè)array:
- NSMutableArray *items;
ViewController的這個(gè)方法返回?cái)?shù)據(jù)條數(shù): +1是為了顯示"加載更多"的那個(gè)cell
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- int count = [items count];
- return count + 1;
- }
這個(gè)方法定制cell的顯示, 尤其是"加載更多"的那個(gè)cell:
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- if([indexPath row] == ([items count])) {
- //創(chuàng)建loadMoreCell
- return loadMoreCell;
- }
- //create your data cell
- return cell;
- }
還要處理"加載更多"的那個(gè)cell的選擇事件,觸發(fā)一個(gè)方法來加載更多數(shù)據(jù)到列表
- - (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的事件
- }
加載數(shù)據(jù)的方法:
- -(void)loadMore
- {
- NSMutableArray *more;
- //加載你的數(shù)據(jù)
- [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
- }
添加數(shù)據(jù)到列表:
- -(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];
- }
小結(jié):詳解iPhone Tableview分批顯示數(shù)據(jù)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!