iOS development: UITableView Basic Use Method

Source: Internet
Author: User
Tags set background

UITableView Basic Use Method

1. First, controller needs to implement two delegate, namely Uitableviewdelegate and Uitableviewdatasource

2. Then the delegate of the UITableView object is set to self.

3. Then you can implement some of these delegate methods to pull.

(1)-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView;

This method returns how many section TableView has

How many sections are returned?

-(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, which 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 height of the footer view 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 for the specified section

-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section

{

if (section ==0)

return 80.0f;

Else

return 30.0f;

}

(5)-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section

Returns the title of the header of the specified section, and if the section header returns view, then 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, and if not, this function does not return to 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

When a user selects a cell in a row, the callback uses this. But first, you must set a property of TableView to be able to select the row.

Tableview.allowsselection=yes;

Cell.selectionstyle=uitableviewcellselectionstyleblue;

If you do not 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, which section is based on which row itself responds.

-(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 to let the cell respond to select, but the color does not change after the selection, then set

Cell.selectionstyle = Uitableviewcellselectionstylenone;

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath

{

The color of the cell is not changed when it is selected

Cell.selectionstyle = Uitableviewcellselectionstylenone;

(8) How to set the TableView line between each line

Self.tableview.separatorstyle=uitableviewcellseparatorstylesingleline;

If you do not need to split the line, then set the property to Uitableviewcellseparatorstylenone can be.

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

(a)-(void) TableView: (UITableView *) TableView Accessorybuttontappedforrowwithindexpath: (Nsindexpath *) IndexPath

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

(11) How to set TableView can be edited

The first step is to enter 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 the type of edit that the current cell is to perform, and 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 is edited, and corresponds to the above code, where we perform the deletion of the cell in this function.

-(void) TableView: (UITableView *) Atableview

Commiteditingstyle: (Uitableviewcelleditingstyle) Editingstyle

Forrowatindexpath: (Nsindexpath *) Indexpath

{

[Chatarray RemoveObjectAtIndex:indexPath.row];

[Chattableview Reloaddata];

(12) How to get a Cell object in 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.