Common properties and methods of iOS development UI Chapter-uitableview

Source: Internet
Author: User
Tags set background

UITableView

The UITableView has two styles built in: uitableviewstyleplain,uitableviewstylegrouped

The <UITableViewDataSource,UITableViewDelegate> method:

TableView process Steps

#pragma Mark 1. How many groups are there?

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView

#pragma mark 2. How high is the header control of section group

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

#pragma Mark 3. How many lines are there in section group

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

How high is the cell in the #pragma mark 4.indexPath?

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

#pragma what the cell looks like in Mark 5.indexPath.

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

#pragma mark 6. What controls are displayed on the Head of section group

-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

Each time a cell enters the field of view the screen is called, so it needs to be optimized inside this method.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

if (Cell==nil) {

Do the work of creating in this. Cycle optimization. Prevents duplicate creation when the cell is flushed into the screen

}

}

When calling Reloaddata, all methods in the calling data source will be refreshed again, and nothing else will be done.

[Self reloaddata]

This method will only be called once, but it will be called only once for each reloaddata of the number of data at the beginning.

It also calculates the height of all cells at once, such as 100 data, one-time invocation of 100

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView//Right Index

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath//Row Click event

Nsindexpath *path = [Self.tableview indexpathforselectedrow]; Get the selected Indexpath to get Section,row

[Self.tableview Reloadrowsatindexpaths:[self.tableview indexpathsforselectedrows] WithRowAnimation: Uitableviewrowanimationnone]; Refreshes table-specified row data

[Self.tableview Reloaddata]; Refresh data for all rows in table

UITableView Common Properties:

UITableView *tableview = [[UITableView alloc]initwithframe:cgrectmake (0, 0, +, 460) Style:uitableviewstyleplain]; Initializing a table

Separator Line Properties

Tableview.separatorstyle = Uitableviewcellseparatorstylesingleline; Uitableviewcellseparatorstylenone;

[Self.tableview Setseparatorstyle:uitableviewcellseparatorstylenone]; Cancel Divider Line

Tableview.separatorcolor = [Uicolor Lightgraycolor];

Item multiple selection

Tableview.allowsmultipleselection = YES;

Set header row Height

[_tableview Setsectionheaderheight:kheaderheight];

[_tableview setsectionfooterheight:0];

Set table Row Height

[_tableview setrowheight:50];

Set Background color

Self.tableView.backgroundView priority is high, if you want to set the BackgroundColor, you must first set the view to nil

Self.tableView.backgroundColor

Add viewon tableView 's head or tail , Footerview width is not set

Xxxview.bounds = CGRectMake (0,0,0,height);

Self.tableView.tableFooterView =xxxview;

Self.tableView.tableHeaderView =xxxview;

UIButton *BT = (uibutton*) [Self.contentview viewwithtag:i+100];

Add tableview scrolling area

Self.tableView.contentInset = uiedgeinsetsmake (0, 0, xx, 0);

UITableViewCell

Create UITableViewCell

UITableViewCell *cell = [[UITableViewCell alloc]

Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:nil];

[Cell.textlabel setbackgroundcolor:[uicolor clearcolor]];//empty label background color

Cell.backgroundview =xx; Set a background picture

Cell.backgroundvcolor =xx;

Cell.selectedbackgroundview = Selectedbgview; Set the background color when selected

Cell.accessoryview = Xxxview; set the right view

[Cell Setaccessorytype:uitableviewcellaccessorynone]; Set RIGHT arrow

[Self setselectionstyle:uitableviewcellselectionstylenone]; Check Style

Cell.selectionstyle = Uitableviewcellselectionstyleblue;

Set the cell height

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

Contentview has 3 sub-views by default, 2 of which are Uilabel, accessed through Textlabel and Detailtextlabel properties, and the 3rd is Uiimageview, accessed through the ImageView property.

Uitableviewcellstyledefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, Uitableviewcellstylesubtitle

#pragma mark-readjust the layout of controls in Uitalbleviewcell

-(void) layoutsubviews{

[Super Layoutsubviews];

...

}

There's a contentview inside the cell.

UITableViewCellTable Optimization

How the UITableViewCell object is reused:

Reuse principle: When scrolling the list, some UITableViewCell will move out of the window, and UITableView will place the UITableViewCell outside the window into an object pool for reuse. When UITableView requires DataSource to return UITableViewCell, DataSource will look at the object pool first, if there are unused uitableviewcell in the pool, DataSource will configure the UITableViewCell with the new data and return it to the UITableView and back to the window to avoid creating new objects

There is also a very important question: Sometimes you need to customize UITableViewCell (inherit UITableViewCell with a subclass), and each line is not necessarily the same uitableviewcell (such as SMS chat layout), So a uitableview may have different types of uitableviewcell, and there will be many different types of UITableViewCell in the object pool, May get the wrong type of UITableViewCell then UITableView is reusing UITableViewCell. Solution:UITableViewCell has a nsstring *reuseidentifier attribute, You can set Reuseidentifier (typically UITableViewCell class name) by passing in a specific string identifier when initializing UITableViewCell. When UITableView requires DataSource to return UITableViewCell, a string is first identified to the object pool to find the corresponding type of UITableViewCell object, if there is, reuse, if not, The string identifier is passed in to initialize a UITableViewCell object

/**

Cell optimization

1. The identifier is unified, the purpose of using static can guarantee that the table identifier will always have only one

2. First find the Cell object named "MyCell" in the buffer pool

3. If not found, instantiate a new cell

**/

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

static NSString *cellidentifier = @ "MyCell";

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

Use this method without judging the cell below

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier Forindexpath:indexpath];

if (cell = = nil) {

cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];

}

return cell;

}

editing mode for tables

Delete, insert

-(void) setediting: (BOOL) editing animated: (bool) animated; Turn on table editing status

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{

Returns the table edit edit style. Do not implement the default is delete

Return Editingstyle:uitableviewcelleditingstyledelete, Uitableviewcelleditingstyleinsert

}

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{

Whether to delete or add operations based on Editingstyle processing

Complete the Delete, insert Operation Refresh Table

-(void) Insertrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;

-(void) Deleterowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;

}

Move

-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: ( Nsindexpath *) Destinationindexpath

Sourceindexpath Moving rows

Destinationindexpath the line of the target

Custom table Row UITableViewCell

Storyboard Mode creation:

Drag directly into uitableview to set UITableViewCell

Attention:

1. When customizing cells by xib or storyboard, you need to specify the reusable identifier of the cells in xib and Storyboard identifier

2. note the differences in the optimization of the table

The two equivalents in storyboard

Xxcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

Xxcell *cell1 = [TableView dequeuereusablecellwithidentifier:cellidentifier Forindexpath:indexpath];

There are differences in the xib file:

In the first case, it can only be used on iOS 6, if the nib file is registered in Viewdidload and a reusable identifier for "cell" is specified, then

Dequeuereusablecellwithidentifier:

Dequeuereusablecellwithidentifier:forindexpath:

The method is equivalent. If the nib file is registered in Viewdidload, the management of the table buffer pool, there is a system takeover!

In the second case, it is available in iOS 4 or above, and if you do not register the nib file with Viewdidload, you can only use

Dequeuereusablecellwithidentifier: and needs to be judged that the cell is not instantiated and handled accordingly

Differences in code creation:

Using code to create the processing in the cell and nib , registering the cell has a system takeover and can be used with Forindexpath method, no registration will have to instantiate the cellitself, can not be used with forindexpath method

[TableView Registerclass:xxxcell class] forcellreuseidentifier:@ "Xxcell"];

Xib Mode creation:

Register identifier

-(void) viewdidload{

[Super Viewdidload];

/**

Note: The following sentence register Xib code, must be in the Viewdidload!

Register the Xib file, get the root view, and convert to TableView for TableView registration Xib

The identifier name is defined in the Xib file and remains consistent

**/

uinib *nib = [uinib nibwithnibname:@ "Bookcell" Bundle:[nsbundle Mainbundle]];

UITableView *tableview = (UITableView *) Self.view;

[TableView registernib:nib forcellreuseidentifier:@ "Bookcell"];

}

No registration identifier can only use the following methods

static NSString *cellidentifier = @ "Bookcell";

Bookcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

if (cell = = nil) {

cell = [[Bookcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];

NSBundle *bundle = [NSBundle mainbundle];

Nsarray *array = [Bundle loadnibnamed:@ "Bookcell" Owner:nil Options:nil];

cell = [array lastobject];

}

Code Mode creation:

1. Build UITableViewCell class, Inherit UITableViewCell

2. Note When adding a view to the cell :

New component is placed in Contentview

[Self.contentview Addsubview:xxview];

Set Picture Extrude Properties Stretch

UIImage *normalimage = [UIImage imagenamed:@ "Xx.png"];

Normalimage = [Normalimage stretchableimagewithleftcapwidth:

NORMALIMAGE.SIZE.WIDTH/2 TOPCAPHEIGHT:NORMALIMAGE.SIZE.HEIGHT/2];

Inside the TableView, viewdidload inside to register the cell class

[TableView Registerclass:xxxcell class] forcellreuseidentifier:@ "Xxcell"];

In a custom tableHeader

Custom tables are defined in this method

-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section

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

Common properties and methods of iOS development UI Chapter-uitableview

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.