Tableview 更多数据的显示方法

来源:互联网 发布:ipad删除软件 编辑:程序博客网 时间:2024/05/17 13:38

具体的思路:

1. 假设每次从服务器拉取的数据有N条,存储在items数组中。

2.在didselect方法中判断所点击的cell是否是最后一条,如果不是则按照正常流程处理。如果是则在后台开辟个线程,向服务器拉去数据,拉取完毕后返回主线程更新UI。


此时值得注意的是如何将新拉取出来的数据重新加载到Tableview中。

具体的方法:1.将新拉取出来的数据添加到原来的items中。

2.根据新拉取的数据判断该数据在新的items中的index,然后拼成indexpath,并存储在indexpathsArray中。

3.调用tableview的插入方法。


具体代码的实现如下:

#import <UIKit/UIKit.h>@interface iphone_tableMoreViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{         IBOutlet UITableView *myTableView;     NSMutableArray *items; } @property (nonatomic,retain) UITableView *myTableView; @property (nonatomic,retain) NSMutableArray *items; @end#import "iphone_tableMoreViewController.h" @implementation iphone_tableMoreViewController @synthesize items,myTableView; - (void)viewDidLoad {     [super viewDidLoad];     items=[[NSMutableArray alloc] initWithCapacity:0];     for (int i=0; i<10; i++) {         [items addObject:[NSString stringWithFormat:@"cell %i",i]];     } } - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning]; }- (void)viewDidUnload {     items=nil;     self.myTableView=nil; } - (void)dealloc {     [self.myTableView release];     [items release];     [super dealloc]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     int count = [items count];     return  count + 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *tag=@"tag";     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];     if (cell==nil) {         cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero                                      reuseIdentifier:tag] autorelease];     }        if([indexPath row] == ([items count])) {         //创建loadMoreCell         cell.textLabel.text=@"More..";     }else {     cell.textLabel.text=[items objectAtIndex:[indexPath row]];        }     return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {        if (indexPath.row == [items count]) {         UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath];         loadMoreCell.textLabel.text=@"loading more …";         [self performSelectorInBackground:@selector(loadMore) withObject:nil];        [tableView deselectRowAtIndexPath:indexPath animated:YES];         return;     }     //其他cell的事件     } -(void)loadMore {     NSMutableArray *more;     more=[[NSMutableArray alloc] initWithCapacity:0];     for (int i=0; i<10; i++) {         [more addObject:[NSString stringWithFormat:@"cell ++%i",i]];     }     //加载你的数据     [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];     [more release]; } -(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.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];     } @end


原创粉丝点击