实现类似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
相关文章推荐
- iOS 中使用tableView实现右滑显示选择功能
- Android使用RecyclerView实现上拉加载更多,下拉刷新,分组显示
- Android中SpannableString学习以及实现自定义TextView的显示更多(展开)和收起功能
- cocos2dx tableview基础:实现多图片的滑动显示
- 仿IOS Tableview 实现类似设置界面
- iOS 类似美团外卖app两个tableView联动效果实现
- css实现table中td单元格鼠标悬浮时显示更多内容
- 【转】Android 使用ViewPager实现类似gallery画廊的效果(画廊效果之ViewPager显示多个图片)
- iOS TableView实现上拉加载更多数据
- 【代码笔记】点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。
- 上拉加载更多FJPullTableView文件的实现代码
- 实现collectionview加载到tableview的tableFooterView上一起滑动(类似淘宝购物车)--通过添加观察者实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- 【代码笔记】点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。
- 实现类似QQ聊天列表的segment点击切换tableView的数据功能
- qt利用委托:QStyledItemDelegate,实现tableview 的表格项的自定义显示,如插入图片,绘制控件
- 动态计算TextView宽度,点击展开更过,类似滴滴打车标题点击弹窗显示更多
- SwipeRefreshLayout配合RecyclerView实现下拉刷新和上拉加载更多以及没有数据的显示
- 实现解析数据table表最后的点击显示更多数据的代码