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
- // return how many Sections there are
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
This method returns how many elements the corresponding section has, that is, how many lines.
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 10;
- }
(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.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
- UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
- if (cell == nil)
- {
- // Create a cell to display an ingredient.
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
- reuseIdentifier: showUserInfoCellIdentifier]
- autorelease];
- }
- // Configure the cell.
- cell.textLabel.text=@ "Signature" ;
- cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
- }
(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Returns the height of the header of the specified section
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- if (section ==0)
- return 80.0f;
- else
- return 30.0f;
- }
(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.
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- if (tableView == tableView_)
- {
- if (section == 0)
- {
- return @"title 1";
- }
- else if (section == 1)
- {
- return @"title 2";
- }
- else
- {
- return nil;
- }
- }
- else
- {
- return nil;
- }
- }
(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
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
- if (section == 0)
- {
- UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
- owner: self
- options: nil] lastObject];
- else
- {
- return nil;
- }
- }
(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.
- TableView.allowsSelection=YES;
- cell.selectionStyle=UITableViewCellSelectionStyleBlue;
If you don't want to respond to select, you can set the property with the following code:
- TableView.allowsSelection=NO;
The following is the response to the select click function, according to which section, which row responds by itself.
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (indexPath.section == 1)
- {
- return;
- }
- else if(indexPath.section==0)
- {
- switch (indexPath.row)
- {
- //to chat with
- case 0:
- {
- [self onTalkToFriendBtn];
- }
- break;
- default:
- break;
- }
- }
- else
- {
- return ;
- }
- }
How to make the cell respond to selection, but the selected color does not change, then set
cell.selectionStyle = UITableViewCellSelectionStyleNone;
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //The color of the cell does not change after it is selected
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }
(9) How to set the dividing line between each row of tableview
- 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
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //set background color
- cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
- }
(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:
- [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
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleDelete;
- }
-(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.
- -(void) tableView:(UITableView *)aTableView
- commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
- forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [chatArray removeObjectAtIndex:indexPath.row];
- [chatTableView reloadData];
- }
(13) How to get the CELL object of a row
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;