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
- Tableview 更多数据的显示方法
- 详解iPhone Tableview分批显示数据 实现点击加载更多
- 实现类似app store的tableview的显示更多
- tableView reload 加载更多数据,tableView 下面会出现向上弹动的不自然效果
- tableview分批显示数据
- IOS tableView数据显示
- tableview的右拉滑动手势的时候显示出更多的按钮+demo
- 【代码笔记】iOS-点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。
- iOS开发>>>UITableview实现更多数据显示方法
- tableView点击下拉更多的效果
- iPhone tableview分批显示数据
- 【學習筆記】tableview分批显示数据
- SWT 中TableView显示数据
- textview显示更多的
- iOS 不同按钮点击显示不同的tableview数据
- iOS 类似股票数据显示的可上下左右滚动TableView
- tableView的显示步骤
- tableView视图列表3: 把解析到的XML数据显示在TableView上
- C++与Java的探戈
- wpa_supplicant无线网络配置
- uva572 - Oil Deposits 油矿
- Oracle_DAY02
- SharePoint 2010 日历颜色
- Tableview 更多数据的显示方法
- NB学校的NB课程的NB教材——CSAPP
- DOCSIS config file-General Packet Classifier Encodings
- 执行计划---索引扫描
- 取球博弈--蓝桥杯
- mysql服务启动、关闭
- 求二叉树中两结点的最小公共祖先
- open与fopen对比
- 0421周赛 HDU 1498 二分匹配