iPhone中使用UITableView實現分頁效果(附代碼)

來源:互聯網
上載者:User

iPhone中使用UITableView實現分頁效果是本文要介紹的內容,UITableview 能夠列表顯示許多內容,也是我們開發中經常用的一個組件。我們經常會分頁顯示列表,如先顯示 10 條記錄,點擊更多在添加 10 條,以此類推,下面是實作類別似更多顯示的一個 demo。

實現的效果如下:

點擊 “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實現分頁效果附代碼)的內容介紹完了,希望通過本文的學習對你有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.