Tableview 更多数据的显示方法
2013-04-24 23:40
483 查看
具体的思路:
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
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
相关文章推荐
- 用json方法解析本地数据,并显示在tableView上面
- iOS开发>>>UITableview实现更多数据显示方法
- DZNEmptyDataSet 当没有数据时tableview,collection的显示
- tableView reload 加载更多数据,tableView 下面会出现向上弹动的不自然效果
- 如何一直显示tableview的滚动条(自定义方法)
- JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)
- UITableView 查询结果为空,显示无数据的方法
- 解决 通过继承QAbstractTableModel并实现headerData方法给QTableView添加标题时,标题不显示 的问题
- 使tableview每个section显示不同行数的方法
- SwipeRefreshLayout配合RecyclerView实现下拉刷新和上拉加载更多以及没有数据的显示
- tableview 选中一行后,不显示选中颜色的方法 tableview 选中一行后,不显示选中颜色,不要将tableview的allowsSelection设置成NO,那样的话可能导致tablev
- 在tableview索引中显示搜索符号的方法
- QTableview QSqlTableModel如何最恰当地只显示一个表中的某几个字段的数据
- iOS 类似股票数据显示的可上下左右滚动TableView
- 实现类似app store的tableview的显示更多
- 在tableview索引中显示搜索符号的方法
- 加载更多时,判断tableView快要滑动到底部的时候在去请求更多数据的公式
- 当数据量很少的时候,tableview会显示多余的cell--iOS开发系列---项目中成长的知识二
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- tableView设置cell高度及分组模式添加头与脚显示方法