UItableView实现分页效果(点击more再显示10条)
2012-01-08 00:27
639 查看
UITableview 能够列表显示许多内容,也是我们开发中经常用的一个组件。我们经常会分页显示列表,如先显示 10条记录,点击更多在添加 10 条,以此类推,下面是实现类似更多显示的一个 demo。
关键再在于函数
实现的效果如下:self.myTableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];的应用
实现的效果如下:
点击 “More…”,实现后面的效果.
实现的思路:
基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中.。
处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表。
indexPathForRow插入数据。
#import<UIKit/UIKit.h>
@interfaceiphone_tableMoreViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
IBOutletUITableView *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 {
[superviewDidLoad];
items=[[NSMutableArray alloc] initWithCapacity:0];
for (inti=0; i<10; i++) {
[items addObject:[NSString stringWithFormat:@"cell %i",i]];
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
- (void)viewDidUnload{
items=nil;
self.myTableView=nil;
}
- (void)dealloc {
[self.myTableView release];
[itemsrelease];
[superdealloc];
}
-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {
int count =[items count];
return count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *tag=@"tag";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier: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:[indexPathrow]];
}
returncell;
}
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) {
UITableViewCell
*loadMoreCell=[tableViewcellForRowAtIndexPath:indexPath];
loadMoreCell.textLabel.text=@"loading
more …";
[self
performSelectorInBackground:@selector(loadMore)withObject:nil];
[tableView
deselectRowAtIndexPath:indexPathanimated:YES];
return;
}
//其他cell的事件
}
-(void)loadMore
{
NSMutableArray *more;
more=[[NSMutableArray alloc] initWithCapacity:0];
for (inti=0; i<10; i++) {
[more
addObject:[NSString stringWithFormat:@"cell ++%i",i]];
}
//加载你的数据
[selfperformSelectorOnMainThread:@selector(appendTableWith:)withObject:more waitUntilDone:NO];
[morerelease];
}
-(void) appendTableWith:(NSMutableArray *)data
{
for (inti=0;i<[data count];i++) {
[items
addObject:[data objectAtIndex:i]];
}
NSMutableArray *insertIndexPaths = [NSMutableArrayarrayWithCapacity:10];
for (int ind= 0; ind < [data count]; ind++) {
NSIndexPath *newPath
= [NSIndexPath indexPathForRow:[itemsindexOfObject:[data objectAtIndex:ind]] inSection:0];
[insertIndexPaths
addObject:newPath];
}
[self.myTableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];
}
@end
关键再在于函数
实现的效果如下:self.myTableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];的应用
实现的效果如下:
点击 “More…”,实现后面的效果.
实现的思路:
基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中.。
处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表。
indexPathForRow插入数据。
#import<UIKit/UIKit.h>
@interfaceiphone_tableMoreViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
IBOutletUITableView *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 {
[superviewDidLoad];
items=[[NSMutableArray alloc] initWithCapacity:0];
for (inti=0; i<10; i++) {
[items addObject:[NSString stringWithFormat:@"cell %i",i]];
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
- (void)viewDidUnload{
items=nil;
self.myTableView=nil;
}
- (void)dealloc {
[self.myTableView release];
[itemsrelease];
[superdealloc];
}
-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {
int count =[items count];
return count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *tag=@"tag";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier: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:[indexPathrow]];
}
returncell;
}
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) {
UITableViewCell
*loadMoreCell=[tableViewcellForRowAtIndexPath:indexPath];
loadMoreCell.textLabel.text=@"loading
more …";
[self
performSelectorInBackground:@selector(loadMore)withObject:nil];
[tableView
deselectRowAtIndexPath:indexPathanimated:YES];
return;
}
//其他cell的事件
}
-(void)loadMore
{
NSMutableArray *more;
more=[[NSMutableArray alloc] initWithCapacity:0];
for (inti=0; i<10; i++) {
[more
addObject:[NSString stringWithFormat:@"cell ++%i",i]];
}
//加载你的数据
[selfperformSelectorOnMainThread:@selector(appendTableWith:)withObject:more waitUntilDone:NO];
[morerelease];
}
-(void) appendTableWith:(NSMutableArray *)data
{
for (inti=0;i<[data count];i++) {
[items
addObject:[data objectAtIndex:i]];
}
NSMutableArray *insertIndexPaths = [NSMutableArrayarrayWithCapacity:10];
for (int ind= 0; ind < [data count]; ind++) {
NSIndexPath *newPath
= [NSIndexPath indexPathForRow:[itemsindexOfObject:[data objectAtIndex:ind]] inSection:0];
[insertIndexPaths
addObject:newPath];
}
[self.myTableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationFade];
}
@end
相关文章推荐
- TextView属性android:ellipsize实现跑马灯效果,TextView内容过长加省略号,点击显示全部内容
- IOS 之 UITableView 实现点击展开裂缝效果
- UITableView实现分页显示
- UITableView 实现分页显示的代码
- UItableView实现分页效果(…
- Android中使用自定义selector来实现view的点击效果以及view的显示样式
- [转]UITableView 实现分页显示的代码
- UITableView 实现分页显示的代码
- iphone开发 UITableview实现分页显示方法(代码)
- iOS-UITableView 实现分页显示的代码
- 怎样实现textview里显示的缩略图,点击查看大图的效果
- javascript点击按钮实现隐藏显示切换效果
- jQuery实现“分页”的效果,控制div分页显示
- Android 5.0 Material Design的实现点击任意View的水波效果
- iOS使用EGO实现UITableView或UICollectionView横向分页刷新
- 学习笔记一、UITableView上下滚动导航栏渐变隐藏和显示效果
- 代码实现UITableView点击cell跳转
- ios8 Swift UICollectionView Header 常驻(如UITableView 的Header效果)实现
- android TextView 实现自定义文字点击效果
- Android自定义控件View(实现控件的动画效果,自定义类画简略时钟,TextView中文字逐一显示,动态设置progress的圆环/扇形ProgressBar等)