UITableView 基本使用方法總結

    1.   首先,Controller須要實現兩個  delegate ,分別是  UITableViewDelegate 和  UITableViewDataSource app

 

   2.而後 UITableView對象的 delegate要設置爲 self。 函數

 

   3. 而後就能夠實現這些delegate的一些方法拉。 spa

 

       (1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;    .net

         這個方法返回 tableview 有多少個section  對象

 

        

[cpp]  view plain copy
  1. //返回有多少個Sections  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
  3. {  
  4.     return 1;  
  5. }  
 


 

         (2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section; blog

        這個方法返回   對應的section有多少個元素,也就是多少行。 ip

        

[cpp]  view plain copy
  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  2. {  
  3.     return 10;  
  4. }  
 


 

         (3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath; ci

                  這個方法返回指定的 row 的高度。 get

                - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; string

                  這個方法返回指定的 section的header view 的高度。

                - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

                  這個方法返回指定的 section的footer view 的高度。

 

          (4)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

                返回指定的row 的cell。這個地方是比較關鍵的地方,通常在這個地方來定製各類個性化的 cell元素。這裏只是使用最簡單最基本

                的cell 類型。其中有一個主標題 cell.textLabel 還有一個副標題cell.detailTextLabel,  還有一個 image在最前頭 叫 

                cell.imageView.  還能夠設置右邊的圖標,經過cell.accessoryType 能夠設置是飽滿的向右的藍色箭頭,仍是單薄的向右箭頭,

                仍是勾勾標記。  

 

               

[cpp]  view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  2. {  
  3.     static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  
  4.     UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];  
  5.     if (cell == nil)  
  6.     {  
  7.         // Create a cell to display an ingredient.  
  8.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   
  9.                                        reuseIdentifier:showUserInfoCellIdentifier]   
  10.                 autorelease];  
  11.     }  
  12.       
  13.     // Configure the cell.  
  14.     cell.textLabel.text=@"簽名";  
  15.     cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];  
  16.         }  
  17.           
 


 

 

             (5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

               返回指定的 section 的header的高度

 

                

[cpp]  view plain copy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  2. {  
  3.     if (section ==0)  
  4.         return 80.0f;  
  5.     else  
  6.         return 30.0f;  
  7. }  
 


 

              (6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

               返回指定的section 的 header  的 title,若是這個section header  有返回view,那麼title就不起做用了。

 

                

[cpp]  view plain copy
  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  2. {  
  3.     if (tableView == tableView_)  
  4.     {  
  5.         if (section == 0)   
  6.         {  
  7.             return @"title 1";  
  8.         }   
  9.         else if (section == 1)   
  10.         {  
  11.             return @"title 2";  
  12.         }   
  13.         else   
  14.         {  
  15.             return nil;  
  16.         }  
  17.     }   
  18.     else   
  19.     {  
  20.         return nil;  
  21.     }  
  22. }  
 


 

              (7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

                  返回指定的 section header 的view,若是沒有,這個函數能夠不返回view

                

[cpp]  view plain copy
  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  2. {  
  3.     if (section == 0)   
  4.     {  
  5.           
  6.         UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"   
  7.                                                         owner: self  
  8.                                                       options: nil] lastObject];  
  9.        
  10.         else  
  11.         {  
  12.            return nil;  
  13.         }  
  14. }  
 


 

              (8)  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

 

               當用戶選中某個行的cell的時候,回調用這個。可是首先,必須設置tableview的一個屬性爲能夠select 才行。

 

 

              

[cpp]  view plain copy
  1. TableView.allowsSelection=YES;  
 


 

               

[cpp]  view plain copy
  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;  
 


              若是不但願響應select,那麼就能夠用下面的代碼設置屬性:

              

  1. TableView.allowsSelection=NO;  


 

              下面是響應select 點擊函數,根據哪一個section,哪一個row 本身作出響應就好啦。

              

[cpp]  view plain copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
  2. {  
  3.     if (indexPath.section == 1)   
  4.     {  
  5.         return;  
  6.     }  
  7.     else if(indexPath.section==0)  
  8.     {  
  9.         switch (indexPath.row)   
  10.         {  
  11.             //聊天  
  12.             case 0:  
  13.             {  
  14.                 [self  onTalkToFriendBtn];  
  15.             }  
  16.                 break;  
  17.                   
  18.             default:  
  19.                 break;  
  20.         }  
  21.     }  
  22.     else   
  23.     {  
  24.         return ;  
  25.     }  
  26.       
  27. }  
 


 

              如何讓cell 可以響應 select,可是選中後的顏色又不發生改變呢,那麼就設置 

              cell.selectionStyle = UITableViewCellSelectionStyleNone;

              

[cpp]  view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     //cell被選中後的顏色不變  
  4.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  5. }  
 


 

            (9)如何設置tableview  每行之間的 分割線

              

[cpp]  view plain copy
  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  
 


               若是不須要分割線,那麼就設置屬性爲 UITableViewCellSeparatorStyleNone  便可。

 

 

             (10)如何設置 tableview cell的背景顏色

              

[cpp]  view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.         //設置背景顏色  
  4.         cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
  5. }  
 


 

             (11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

 

               這個函數響應,用戶點擊cell 右邊的 箭頭(若是有的話)

 

 

            (12)如何設置tableview 能夠被編輯

               首先要進入編輯模式:

              

[cpp]  view plain copy
  1. [TableView setEditing:YES animated:YES];  
 


               若是要退出編輯模式,確定就是設置爲NO

 

                - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

                返回當前cell  要執行的是哪一種編輯,下面的代碼是 返回 刪除  模式

              

[cpp]  view plain copy
  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return UITableViewCellEditingStyleDelete;  
  4. }  
 


 

              -(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

              通知告訴用戶編輯了 哪一個cell,對應上面的代碼,咱們在這個函數裏面執行刪除cell的操做。

相關文章
相關標籤/搜索