iPhone中使用UITableView实现分页效果(附代码)

移动开发 iOS
本文介绍的是iPhone中使用UITableView实现分页效果。详细讲解了UITableView的使用方法,先来看内容详解。

iPhone中使用UITableView实现分页效果是本文要介绍的内容,UITableview 能够列表显示许多内容,也是我们开发中经常用的一个组件。我们经常会分页显示列表,如先显示 10 条记录,点击更多在添加 10 条,以此类推,下面是实现类似更多显示的一个 demo。

实现的效果如下:

iPhone中使用UITableView实现分页效果

点击 “More…”,实现后面的效果.

实现的思路:

基本上就是数据源里先只放10条, 点击***一个cell时, 添加更多的数据到数据源中。

处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表。

indexPathForRow插入数据。

实现过程如下:

  1. #import <UIKit/UIKit.h> 
  2.  
  3. @interface iphone_tableMoreViewController : UIViewController   
  4. <UITableViewDelegate,UITableViewDataSource>{   
  5.       
  6.     IBOutlet UITableView *myTableView;   
  7.     NSMutableArray *items;   
  8. }   
  9. @property (nonatomic,retain) UITableView *myTableView;   
  10. @property (nonatomic,retain) NSMutableArray *items;   
  11. @end  
  12.  
  13. #import "iphone_tableMoreViewController.h"   
  14. @implementation iphone_tableMoreViewController   
  15. @synthesize items,myTableView;   
  16. - (void)viewDidLoad {   
  17.     [super viewDidLoad];   
  18.     items=[[NSMutableArray alloc] initWithCapacity:0];   
  19.     for (int i=0; i<10; i++) {   
  20.         [items addObject:[NSString stringWithFormat:@"cell %i",i]];   
  21.     }   
  22. }   
  23. - (void)didReceiveMemoryWarning {   
  24.     [super didReceiveMemoryWarning];   
  25. }  
  26.  
  27. - (void)viewDidUnload {   
  28.     items=nil;   
  29.     self.myTableView=nil;   
  30. }   
  31. - (void)dealloc {   
  32.     [self.myTableView release];   
  33.     [items release];   
  34.     [super dealloc];   
  35. }  
  36.  
  37. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   
  38.     int count = [items count];   
  39.     return  count + 1;   
  40. }   
  41. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
  42.     static NSString *tag=@"tag";   
  43.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];   
  44.     if (cell==nil) {   
  45.         cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero   
  46.                                      reuseIdentifier:tag] autorelease];   
  47.     }      
  48.     if([indexPath row] == ([items count])) {   
  49.         //创建loadMoreCell   
  50.         cell.textLabel.text=@"More..";   
  51.     }else {   
  52.     cell.textLabel.text=[items objectAtIndex:[indexPath row]];      
  53.     }   
  54.     return cell;   
  55. }   
  56. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   
  57.       
  58.  
  59.     if (indexPath.row == [items count]) {   
  60.         UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath];   
  61.         loadMoreCell.textLabel.text=@"loading more …";   
  62.         [self performSelectorInBackground:@selector(loadMore) withObject:nil];   
  63.        [tableView deselectRowAtIndexPath:indexPath animated:YES];   
  64.         return;   
  65.     }   
  66.     //其他cell的事件   
  67.       
  68. }   
  69. -(void)loadMore   
  70. {   
  71.     NSMutableArray *more;   
  72.     more=[[NSMutableArray alloc] initWithCapacity:0];   
  73.     for (int i=0; i<10; i++) {   
  74.         [more addObject:[NSString stringWithFormat:@"cell ++%i",i]];   
  75.     }   
  76.     //加载你的数据   
  77.     [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];   
  78.     [more release];   
  79. }   
  80. -(void) appendTableWith:(NSMutableArray *)data   
  81. {   
  82.     for (int i=0;i<[data count];i++) {   
  83.         [items addObject:[data objectAtIndex:i]];   
  84.     }   
  85.     NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];   
  86.     for (int ind = 0; ind < [data count]; ind++) {   
  87.         NSIndexPath    *newPath =  [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];   
  88.         [insertIndexPaths addObject:newPath];   
  89.     }   
  90.    [self.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];   
  91.       
  92. }   
  93. @end 

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.tableMore/

小结:iPhone中使用UITableView实现分页效果(附代码)的内容介绍完了,希望通过本文的学习对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-27 11:19:33

iPhone UITableVie

2011-08-10 14:40:23

iPhone动画

2010-09-17 10:26:01

iPhone

2011-07-08 10:15:15

IPhone 动画

2011-08-18 13:58:34

iPhone开发NSOperation异步

2011-07-20 14:53:28

iPhone NSLocalize 国际化

2011-08-11 13:26:30

iPhoneNSLocalized

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2011-08-02 17:14:41

iPhone应用 UITableVie

2011-07-08 15:08:16

iPhone 图片

2011-07-27 11:14:37

iPhone UITableVie

2011-08-17 14:57:31

iPhone应用视频播放

2011-08-22 14:21:24

iPhone开发UIView Anim

2011-08-12 14:04:53

iPhone动画

2011-08-15 13:44:07

iPhone开发UITableView

2011-08-15 15:26:20

iPhone开发CocoaXML

2013-07-29 14:28:43

JQueryJQuery实现分页分页程序代码

2011-07-29 13:55:10

IPhone 动画

2011-08-16 18:13:42

IPhone开发UIView动画

2011-07-20 15:20:14

IPhone AVAudioRec
点赞
收藏

51CTO技术栈公众号