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

iPhone應用中UITableView用法總結

移動開發(fā) iOS
本文介紹的是iPhone應用中UITableView用法總結,主要是講解對UITableView的理解,先來看內(nèi)容。

iPhone應用UITableView用法總結是本文要介紹的內(nèi)容,這一節(jié)主要講解對UITableView的理解,IPhone的應用程序是離不開UITableView應用程序的,因此理解Table控件特別的重要。下面主要是對UITableView顯示數(shù)據(jù)的兩種方式進行講解。

顯示數(shù)據(jù)的內(nèi)容主要是UITableView控件,但是我們需要定義一個類為他加載數(shù)據(jù),以及定義和用戶交互交互的回調(diào)函數(shù),這兩個方面是通過委托的形式實現(xiàn)的。根據(jù)MVC的設計形式,Apple將UITableView放置到一個UITableViewController類當中,使用UITableViewController類實例來控制UITableView的更個方面的屬性。我們需要創(chuàng)建這么一個controller類型的類,這種類可以通過繼承成為UITableView的父類,也可以不繼承這個類,僅僅實現(xiàn)數(shù)據(jù)源委托以及操作委托即可。

1、創(chuàng)建基于UITableViewController的子類。

  1. @interface RootViewController : UITableViewController {  
  2. NSArray *dataArray ;  
  3. }  
  4. @property(nonatomic,retain) NSArray *dataArray ; 

因為UITableViewController類已經(jīng)包含了委托<UITableViewDelegate, UITableViewDataSource>,因此我們在這里就不需要手動添加這些委托。我們可以通過查看源代碼來查看我們的假設:

  1. UIKIT_EXTERN_CLASS @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
  2. {  
  3.   @private  
  4.     UITableViewStyle _tableViewStyle;  
  5.     id               _keyboardSupport;  
  6.     struct {  
  7.         int clearsSelectionOnViewWillAppear:1;  
  8.     } _tableViewControllerFlags;  

通過上面的代碼我們可以看到UITableViewController已經(jīng)繼承了<UITableViewDelegate, UITableViewDataSource>這兩個委托。

這樣以來,在實現(xiàn)文件里面我們僅僅需要實現(xiàn)這兩個委托里面的必須實現(xiàn)的函數(shù)就可以了。

但是,到這里我們還是不可以的,我們還沒有完完成任務。 我們創(chuàng)建的這個類還是需要一個能夠裝載這個類的實例的容器來著,這就好像,我們?nèi)ベI一瓶礦泉水,水是必須裝在一個瓶子(容器)里面才可以拿著喝得。 我們創(chuàng)建的這個類的實例,就好比水,我們還需要創(chuàng)建一個瓶子(容器)來裝這些水 。這個容器可以是一個UIView實例,或者是一個UINavigationController實例,或者是一個Nib文件都可以,這些準備完畢后就可以看到效果了。

2、現(xiàn)在我們來看第二種情況,創(chuàng)建普通控制類的實例。 

如果我們創(chuàng)建普通控制類的實例,比如如下面所示:

  1. @interface RootViewController:UITableViewController  
  2. <UITableViewDelegate,UITableViewDataSource> 
  3. {  
  4. NSArray *dataArray;  
  5. }  
  6. @property(nonatomic,retain) NSArray *dataArray ; 

這種情況下,我們需要手動的添加來繼承委托,實現(xiàn)數(shù)據(jù)源的繼承以及數(shù)據(jù)操作的繼承。

然后剩下的情況就和其他上面的一樣來,實現(xiàn)委托中定義的必須要實現(xiàn)的類,加載數(shù)據(jù),加載數(shù)據(jù)到容器中,然后顯示數(shù)據(jù)。

創(chuàng)建一個簡單的表格

參考代碼:

  1. //.h  
  2. @interface ViewBaseAppViewController : UIViewController   
  3. <UITableViewDelegate,UITableViewDataSource> 
  4. {  
  5. IBOutlet UITableView *m_tableView;  
  6. }  
  7. //.m  
  8. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  9.     // Return the number of rows in the section.  
  10. return 10;  
  11. }  
  12.  
  13. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  14. {    
  15.     static NSString *CellIdentifier = @"Cell";  
  16.  
  17.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  18.     if (cell == nil) {  
  19.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  20.     }  
  21.     cell.textLabel.text =@"xingchen";  
  22.     return cell;  

小結:iPhone應用UITableView用法總結的內(nèi)容介紹完了,希望本文對你有所幫助。

責任編輯:zhaolei 來源: 博客園
相關推薦

2011-08-15 13:44:07

iPhone開發(fā)UITableView

2011-07-27 11:14:37

iPhone UITableVie

2011-08-15 11:37:20

iPhone開發(fā)Mask

2011-08-03 17:18:58

iPhone UILabel UISlider

2011-08-19 10:01:09

iPhone應用SqliteUITableView

2012-04-26 21:56:59

iPhone

2011-08-17 10:16:35

iPhone應用HTTP請求協(xié)議

2011-07-27 11:19:33

iPhone UITableVie

2011-04-12 11:32:31

Oraclerownum用法

2011-08-08 10:42:46

iPhone UITableVie 分頁

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-02 17:27:06

iPhone應用 剪切技巧

2013-07-25 14:12:53

iOS開發(fā)學習UITableView

2011-05-31 14:33:53

settimeout

2011-04-07 16:34:05

staticC++

2011-04-19 16:38:00

對象指針指針C++

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 15:44:46

iPhone開發(fā)PDF

2011-08-18 16:24:44

iPhone開發(fā)圖片
點贊
收藏

51CTO技術棧公眾號