(transfer) [IOS] Summary of the basic usage of UITableView

Reprinted from: http://blog.csdn.net/tangaowen/article/details/6438362

 

1. First, the Controller needs to implement two delegates, namely   UITableViewDelegate and UITableViewDataSource

 

   2. Then the delegate of the UITableView object should be set to self.

 

   3. Then you can implement some methods of these delegates.

 

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

         This method returns how many sections the tableview has 

 

        

[cpp] view plain copy
 
  1. // return how many Sections there are  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
  3. {  
  4.     return 1;  
  5. }  

 

 

 

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

        This method returns how many elements the corresponding section has, that is, how many lines.

        

[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;

                  This method returns the height of the specified row.

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

                  This method returns the height of the specified section's header view.

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

                  This method returns the height of the footer view of the specified section.

 

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

                Returns the cell of the specified row. This place is a more critical place, and it is generally used to customize various personalized cell elements in this place. Here is just the simplest and most basic

                the cell type. There is a main title cell.textLabel and a subtitle cell.detailTextLabel, and an image at the top called 

                cell.imageView. You can also set the icon on the right. Through cell.accessoryType, you can set whether it is a full right blue arrow or a thin right arrow.

                Still a tick mark.  

 

               

[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=@ "Signature" ;  
  15.     cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];  
  16.         }  
  17.           

 

 

 

 

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

               Returns the height of the header of the specified section

 

                

[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

               Returns the title of the header of the specified section. If the section header returns a view, the title will not work.

 

                

[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

                  Returns the view of the specified section header, if not, this function can not return the 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

 

               This is called back when the user selects a cell in a row. But first, a property of the tableview must be set to be selectable.

 

 

              

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

 

 

 

               

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

 

 

              If you don't want to respond to select, you can set the property with the following code:

              

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

 

 

              The following is the response to the select click function, according to which section, which row responds by itself.

              

[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.             //to chat with  
  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. }  

 

 

 

              How to make the cell respond to selection, but the selected color does not change, then set 

              cell.selectionStyle = UITableViewCellSelectionStyleNone;

              

[cpp] view plain copy
 
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     //The color of the cell does not change after it is selected  
  4.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  5. }  

 

 

 

            (9) How to set the dividing line between each row of tableview

              

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

 

 

               If you don't need a dividing line, set the property to UITableViewCellSeparatorStyleNone.

 

 

             (10) How to set the background color of the tableview cell

              

[cpp] view plain copy
 
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.         //set background color  
  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

 

               This function responds that the user clicks the arrow to the right of the cell (if any)

 

 

            (12) How to set the tableview can be edited

               First go into edit mode:

              

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

 

 

               If you want to exit edit mode, it must be set to NO

 

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

                Returns what kind of editing the current cell is to perform, the following code is to return to delete mode

              

[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

              The notification tells the user which cell has been edited. Corresponding to the above code, we perform the operation of deleting the cell in this function.

 

 

              

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

 

 

 

 

             (13) How to get the CELL object of a row

 

 

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326166927&siteId=291194637
Recommended