UITableView 基本使用方法


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

    2.然後 UITableView對象的 delegate要設置爲 self。

   3. 然後就可以實現這些delegate的一些方法拉。

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

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

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

 

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

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

        

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  2. {  
  3.     return 10;  
  4. }  
 

 

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

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

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

                  這個方法返回指定的 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 可以設置是飽滿的向右的藍色箭頭,還是單薄的向右箭頭,

                還是勾勾標記。  

 

               

  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的高度

  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就不起作用了。

 

                

  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

                

  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 才行。

 

 

              

  1. TableView.allowsSelection=YES;  
 

 

               

  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;  
 

              如果不希望響應select,那麼就可以用下面的代碼設置屬性:

              

  1. TableView.allowsSelection=NO;  

 

              下面是響應select 點擊函數,根據哪個section,哪個row 自己做出響應就好啦。

              

  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;

              

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     //cell被選中後的顏色不變  
  4.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  5. }  
             (9)如何設置tableview  每行之間的 分割線

              

  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  
 

               如果不需要分割線,那麼就設置屬性爲 UITableViewCellSeparatorStyleNone  即可。

 

 

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

              

  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 可以被編輯

               首先要進入編輯模式:

              

  1. [TableView setEditing:YES animated:YES];  
 

               如果要退出編輯模式,肯定就是設置爲NO

 

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

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

              

  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的操作。

  

  1. -(void) tableView:(UITableView *)aTableView  
  2. commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
  3. forRowAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5.         [chatArray  removeObjectAtIndex:indexPath.row];  
  6.     [chatTableView  reloadData];  
  7. }  
  

             (13)如何獲得 某一行的CELL對象

 

               - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章