IOS development: basic usage of UITableView

Source: Internet
Author: User

IOS development: basic usage of UITableView

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.

// Returns the number of Sections S.

-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView

{

Return 1;

}

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

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

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

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

-(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];

}

(4)-(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 80366f;

Else

Return 30366f;

}

(5)-(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.

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

}

}

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

Returns the view of the specified section header. If not, this function does 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;

}

}

(7)-(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.

TableView. allowsSelection = YES;

Cell. selectionStyle = UITableViewCellSelectionStyleBlue;

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

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.

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

{

If (indexPath. section = 1)

{

Return;

}

Else if (indexPath. section = 0)

{

Switch (indexPath. row)

{

// Chat

Case 0:

{

[Self onTalkToFriendBtn];

}

Break;

Default:

Break;

}

}

Else

{

Return;

}

}

How can we make the cell respond to the select statement without changing the selected color?

Cell. selectionStyle = UITableViewCellSelectionStyleNone;

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

{

// The cell color remains unchanged after it is selected

Cell. selectionStyle = UITableViewCellSelectionStyleNone;

}

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

Self. tableView. separatorStyle = UITableViewCellSeparatorStyleSingleLine;

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

(9) how to set the background color of tableview cell

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

{

// Set the background color

Cell. contentView. backgroundColor = [UIColor colorWithRed: 0.957 green: 0.957 blue: 0.957 alpha: 1];

}

(10)-(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)

(11) how to set tableview to be edited

First, enter the editing mode:

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

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

-(Void) tableView :( UITableView *) aTableView

CommitEditingStyle :( UITableViewCellEditingStyle) editingStyle

ForRowAtIndexPath :( NSIndexPath *) indexPath

{

[ChatArray removeObjectAtIndex: indexPath. row];

[ChatTableView reloadData];

}

(12) how to obtain the CELL object of a row

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

Related Article

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.