Summary of the basic usage of UITableView for iOS development

Source: Internet
Author: User

Basic usage of UITableView

1. First, the Controller needs to implement two delegate: UITableViewDelegate and UITableViewDataSource.

2. Set the delegate of the UITableView object to self.

3. Then we can implement some delegate methods.

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

This method returns the number of sections in tableview.

 
 
  1. // Returns the number of Sections S.
  2. -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
  3. {
  4. Return 1;
  5. }

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

This method returns the number of elements in the corresponding section, that is, the number of rows.

 
 
  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 header view of the specified section.

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

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

 
 
  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. }

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

Returns the height of the header of the specified section.

 
 
  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 a view is returned for this section header, the title does not work.

 
 
  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 does not return the 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

This is called when the cell of a row is selected. But first, you must set a tableview attribute to select.

 
 
  1. TableView.allowsSelection=YES; 
 
 
  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;  

If you do not want to respond to the select statement, you can use the following code to set attributes:

 
 
  1. TableView.allowsSelection=NO;  

The following is the response to the select click function. It is okay to choose which section and which row to make the response by yourself.

 
 
  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. // Chat
  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 can we make the cell respond to the select statement without changing the selected color?

Cell. selectionStyle = UITableViewCellSelectionStyleNone;

 
 
  1. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
  2. {
  3. // The cell color remains unchanged after it is selected
  4. Cell. selectionStyle = UITableViewCellSelectionStyleNone;
  5. }

9) how to set the split line between each row in tableview

 
 
  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  

If you do not need to split the line, set the property to UITableViewCellSeparatorStyleNone.

10) how to set the background color of tableview cell

 
 
  1. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
  2. {
  3. // Set the 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

In response to this function, the user clicks the arrow on the right of the cell, if any)

12) how to set tableview to be edited

First, enter the editing mode:

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

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

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

Return the edit type to be executed by the current cell. The following code returns the deletion mode.

 
 
  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 code above, we will delete the cell in this function.

 
 
  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 obtain the CELL object of a row

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.