自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone中使用UITableView實(shí)現(xiàn)分頁效果(附代碼)

移動(dòng)開發(fā) iOS
本文介紹的是iPhone中使用UITableView實(shí)現(xiàn)分頁效果。詳細(xì)講解了UITableView的使用方法,先來看內(nèi)容詳解。

iPhone中使用UITableView實(shí)現(xiàn)分頁效果是本文要介紹的內(nèi)容,UITableview 能夠列表顯示許多內(nèi)容,也是我們開發(fā)中經(jīng)常用的一個(gè)組件。我們經(jīng)常會(huì)分頁顯示列表,如先顯示 10 條記錄,點(diǎn)擊更多在添加 10 條,以此類推,下面是實(shí)現(xiàn)類似更多顯示的一個(gè) demo。

實(shí)現(xiàn)的效果如下:

iPhone中使用UITableView實(shí)現(xiàn)分頁效果

點(diǎn)擊 “More…”,實(shí)現(xiàn)后面的效果.

實(shí)現(xiàn)的思路:

基本上就是數(shù)據(jù)源里先只放10條, 點(diǎn)擊***一個(gè)cell時(shí), 添加更多的數(shù)據(jù)到數(shù)據(jù)源中。

處理"加載更多"的那個(gè)cell的選擇事件,觸發(fā)一個(gè)方法來加載更多數(shù)據(jù)到列表。

indexPathForRow插入數(shù)據(jù)。

實(shí)現(xiàn)過程如下:

  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.         //創(chuàng)建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.     //加載你的數(shù)據(jù)   
  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/

小結(jié):iPhone中使用UITableView實(shí)現(xiàn)分頁效果(附代碼)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-27 11:19:33

iPhone UITableVie

2011-08-10 14:40:23

iPhone動(dòng)畫

2010-09-17 10:26:01

iPhone

2011-07-08 10:15:15

IPhone 動(dòng)畫

2011-08-18 13:58:34

iPhone開發(fā)NSOperation異步

2011-07-20 14:53:28

iPhone NSLocalize 國際化

2011-08-11 13:26:30

iPhoneNSLocalized

2011-08-19 10:01:09

iPhone應(yīng)用SqliteUITableView

2011-08-02 17:14:41

iPhone應(yīng)用 UITableVie

2011-07-27 11:14:37

iPhone UITableVie

2011-07-08 15:08:16

iPhone 圖片

2011-08-17 14:57:31

iPhone應(yīng)用視頻播放

2011-08-15 13:44:07

iPhone開發(fā)UITableView

2011-08-22 14:21:24

iPhone開發(fā)UIView Anim

2011-08-12 14:04:53

iPhone動(dòng)畫

2011-08-15 15:26:20

iPhone開發(fā)CocoaXML

2013-07-29 14:28:43

JQueryJQuery實(shí)現(xiàn)分頁分頁程序代碼

2011-07-20 15:20:14

IPhone AVAudioRec

2011-07-29 13:55:10

IPhone 動(dòng)畫

2009-02-24 11:05:07

ibmdwiphonegoogle
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)