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

iPhone開發(fā)應用中關于UITableView詳細教程

移動開發(fā) iOS
本文主要是來學習UITableView的詳細操作。UITableView是一個很強大的控件,在我們iphone開發(fā)過程中會經(jīng)常用到。

iPhone開發(fā)應用中關于UITableView詳細教程是本文要介紹的內容,主要是來學習UITableView的詳細操作。UITableView是一個很強大的控件,在我們iphone開發(fā)過程中會經(jīng)常用到。

下面我做以下簡單介紹

‍UITableView有一個基本元素的索引NSIndexPath,你可以通過索引NSIndexPath找到‍UITableView下面的子元素只要這個方法

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2.    //在這個方法里你可以根據(jù)NSIndexPath判斷相應的元素,然后做處理 

例如:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. if(indexPath.section == 5 && indexPath.row ==2)return;  
  4. }  
  5. UITableView的每一個子元素(Entity)稱為UITableViewCell,UITableViewCell由下面這個方法初始化  
  6. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath  
  7. //這個方法是必需的,他是產生UITableView內容的必須載體 

例如:

  1. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath  
  2. {//因為UITableView由很好的內存控制機制,他每次只加載一屏幕的cell(7個左右),當用戶觸摸移動時,動態(tài)加載新產生的cell  
  3. static NSString *RootViewControllerCell =@"HelpViewControllerCell";//產生一個靜態(tài)標識  
  4. UITableViewCell* cell =(UITableViewCell*)[tableViewdequeueReusableCellWithIdentifier:RootViewControllerCell];//標記新產生的cell  
  5. if(cell==nil) //如果cell不為空標識cell還沒有被釋放,還在屏幕中顯示  
  6. {  
  7. cell =[[[UITableViewCellalloc]initWithStyle:UITableViewCellSeparatorStyleSingleLinereuseIdentifier:RootViewControllerCell]autorelease];  
  8. }  
  9. return cell;  
  10. } 

UITableViewCell通過NSIndexPath索引,正如上面所看到的,NSIndexPath索引由兩個屬性section和row

section通常就是我們所謂的段,row所謂的段內的行

我們可以通過下面這個方法返回你想要在UITableView中顯示段數(shù)

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //通常情況下我們看到的都是一段,左移這個方法不是必須的 

我們可以通過下面這個方法返回你想要在UITableView中的某一段中顯示的行數(shù)

  1. - (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section;//通常情況下是一段,所以不必判斷段數(shù) 

假如您有很多段,而且每一個段的顯示行數(shù)還不一樣,你就要通過上面的方法精確控制,例如:

  1. - (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {  
  2.       if(section == 0) return 1;  
  3.       if(section == 1) return 1;  
  4.    if(section == 2) return 8;  
  5.    if(section == 3) return 1;  
  6.    if(section == 4 || section ==5)   return 3;  
  7.    if(section == 6)return 4;  
  8.    return0;  

這個方法時必須的。

另外我們可以通過下面這個方法精確控制某一行的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.  
  3.    if(indexPath.section == 0 && indexPath.row ==0)    return80;  
  4.    return 40.0;  

另外我們可以通過下面這個方法精確控制某一段的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  2. {  
  3.    if (section == 0)return60.0;  
  4.    return40.0;  

另外我們還可以通過下面這個方法精確控制某一段的標題和視圖

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;  
  2.  
  3. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 

這兩個方法鄙視必須的,我就不作介紹。

擁有以上的及格方法你就可以完成超炫的UITableView視圖了。前提是要活靈活用,還有其他幾個常用的控件,這里就不在寫了,需要的可以留言交流。

小結:iPhone開發(fā)應用中關于UITableView詳細教程的內容介紹完了,希望本文對你有所幫助!

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

2011-08-02 17:14:41

iPhone應用 UITableVie

2011-07-27 11:14:37

iPhone UITableVie

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-07-06 17:40:43

iPhone SDK

2011-07-08 14:58:16

iPhone Xcode iOS

2011-08-08 14:07:49

iPhone開發(fā) 字體

2011-08-18 10:39:46

iPhone開發(fā)界面

2011-08-12 10:09:23

iPhone開發(fā)多線程

2011-07-27 11:19:33

iPhone UITableVie

2011-08-09 13:10:32

iPhone地圖開發(fā)

2011-08-19 10:35:19

iPhone應用Three20

2011-08-18 10:06:10

2011-08-09 14:42:07

iPhonePCM播放器

2011-08-08 18:19:09

iPhone音頻播放

2011-08-10 10:23:20

iPhoneArchivingNSCoder

2011-07-29 13:27:48

iPhone 開發(fā) Nib

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-18 15:24:40

iPhone國際化

2011-08-22 14:21:24

iPhone開發(fā)UIView Anim

2011-08-22 15:15:49

iPhone開發(fā)NSMutableAr排序
點贊
收藏

51CTO技術棧公眾號