您的位置:首页 > 移动开发

实现类似app store的tableview的显示更多

2012-05-18 09:30 225 查看
用户场景是非常明显的,如果你从服务器端拉一堆数据,比如1000行,回去显示,那么程序很慢,然后用户体验很差//典型的解决方法是,在最后一行显示为load more这样就形成一个block一本block来获取数据。
好处很明显:
1. 节约了网络流量
2. 用户不用等很久看到数据
A:
用判断最后一行,在其中显示信息的,一般比如以每20条出现一次,那么第21条就是 ”显示更多“--Load more items。。。
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row == count) {
cell = [tableView dequeueReusableCellWithIdentifier:moreCellId];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:moreCellId] autorelease];
}
cell.textLabel.text = @"Load more items...";
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
}
B:
用于判断第一行,同时用于展示说数据 还没有(目前)
巧妙地只在第一行 输出“loading......”(没有数据且是第一行的时候)if (nodeCount == 0 && indexPath.row == 0){        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];        if (cell == nil){ cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:PlaceholderCellIdentifier] autorelease];               cell.detailTextLabel.textAlignment = UITextAlignmentCenter;cell.selectionStyle = UITableViewCellSelectionStyleNone; }cell.detailTextLabel.text = @"Loading…";return cell;}
@interface Post : NSObject {NSString *postTitle;NSString *postDescr;NSDate *pubDate;}@property (copy, nonatomic) NSString *postTitle;@property (copy, nonatomic) NSString *postDescr;@property (copy, nonatomic) NSDate *pubDate;@end
@interface Feed : NSObject {}- (NSArray *)newPosts;@end
典型用户场景中分析,两个model,一个纯粹是数据单元就是post,另外一个是feed,其中提供了一个操作,返回新的post,这种是以时间为timestamp的,我们也可以用按队列index的。- (NSArray *)GetPosts(beginIndex,numbers); //会不会导致重复呢?paging的机制?专门用于读的一个表?更新没有那么快?/
- (NSArray *)newPosts {NSMutableArray *posts = [[NSMutableArray alloc] init];for (NSInteger item = 1; item <= 5; item++) {NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];Post *newPost = [[Post alloc] init];NSDate *pubDate = [NSDate dateWithTimeIntervalSinceNow:item];newPost.postTitle = [NSString stringWithFormat:@"Article %@", pubDate];newPost.postDescr = @"Some text describing this post...";newPost.pubDate = pubDate;[posts addObject:newPost];[newPost release];[pool drain];}return posts;}
@interface FeedViewController : UITableViewController {Feed *feed;NSMutableArray *posts;}@property (nonatomic, retain) Feed *feed;@property (nonatomic, retain) NSMutableArray *posts;  //作为数据源
NSArray *newPosts = [feed newPosts];
if (newPosts) {
[self.posts addObjectsFromArray:newPosts];
[newPosts release];
}
我们看上面的是以时间为区分,去获取更多新数据的。。。。更多数据的。通过posts addobjects/addobjectsfromArray完成了数据源的合并,在数据合并后,return总是posts count+1.+1就是为了显示更多//你程序要实现的好一点,最好返回的时候,除了array,带上 是否还有数据,以便在没有数据的时候不要用:load more。。。。。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [posts count] + 1;
}
Hint:samples\Feeder-dynamicData
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐