Objective-c--ui Foundation Development Day Sixth (UITableView)

Source: Internet
Author: User

First, the simple use of UITableView

Display Features:

1, show how many to the District group

2. How many rows of data are displayed

3. What is displayed in each row

The agent will not remind you what method is not called, but Uitableviewdatasource will

1) Create a UITableView with code

UITableView *tableview =[[uitableview Alloc]initwithframe:cgrectmakr (0,0,[uiscreen mainScreen].bounds.size.width,[ UIScreen mainscreen].bounds.size.height) Style:uitableviewstyleplain];

/**

Note Style:

Uitableviewstyleplain

uitableviewstylegrouped

Difference: Plain's footer and header have a floating effect, and there is no interval between them

There is a certain distance between the Grouped:footer and the header

*/

2) Set the Proxy method for TableView datasource

Tableview.datasource =self;

3) Call three proxy methods that must be implemented

3.1) Numberofsectionsintableview return (Nsinteger)

3.2) Numberofrowsinsection return (Nsinteger)

3.3) Cellforrowsatindex return (uitableviewcell*)

4) Note the invocation method of the three proxy methods (when you set 3 sections to display 1,2,2 row rows respectively)

It can be observed that:

Call the section group several times Numberofsectionsintableview method

Each time you confirm the corresponding number of rows for all groups, call Numberofrowsinsection

Sections*rows

Call Sections*rows Secondary Cellforrowsatindex method

Take each row of the corresponding group to the past

5) Summarize the procedure and number of calls to the Proxy method:

Each call gets the total number of groups, then passes in the number of groups, gets the number of rows in the first set, then passes in the number of groups, gets the number of rows in the second set, and so on, and finally, the content is delivered according to each group's corresponding Indexpath property.

6) Set header and footer for TableView section

Titleforfooterinsection/titleforheaderinsection return (nsstring*)

Note that you can also use viewforfooterinsection/viewforheaderinsection different is that you need to define (uitableviewdelegate) This proxy, return (uiview*)

Direct return to display

Second, the car show: UITableView data binding

1) Create model class

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *icon;

-(Instancetype) Initwithdictionary: (nsdictionary*) dict;

+ (Instancetype) Carinitwithdictioary: (nsdictionary*) dict;

2) define the execution model class method

-(Instancetype) Initwithdictionary: (nsdictionary*) dict

{//note, previously wrongly written carmodel. Init error Cannot initialize a class NSObject

if (self =[super init])

{[Self setvaluesforkeywithdictionary:dict];}}

+ (Instancetype) Carinitwithdictionary: (nsdictionary*) Dict{[return [self alloc]initwithdictionary:dict];}

3) Lazy loading, lazy loading is actually redefining its getter method, and can be used to allocate memory, not use when the automatic recovery of memory, save memory consumption, and can avoid repeated instantiation, each instance independent, It also reduces the amount of code that defines parameters directly in the Viewdidload

@property (Nonatomic,strong) Nsarray *dataarray (if the value of DataArray is modified in the future, you need to use Nsmutablearray to invoke the corresponding assignment method addobjects ...)

-(Nsarray *) DataArray

{

NSString *path=[nsbundel Mainbundel] pathforresourct: @ "Car.plist", Oftype:nil];

Nsarray *temparray =[nsarray arraywithcontentsoffile:p Ath];

Nsmutablearray *muarray =[nsmutablearray Array];

For (Nsdictionary *dict in Temparray)

{

Carmodel *model =[carmodel carinitwithdictionary:dict];

[Muarray Addobject:model];

}

_dataarray =muarray;

return _dataarray;

}

Attention

If the TableView is not displayed, consider:

1, whether to add agent tableview.delegate=self;

2, consider whether to take out the data: is the path correct, lazy loading is successful?

Iii. properties of some other TableView and Tableviewcell

1)/*

Cell.accessorytype=uitableviewcellaccessorydisclosureindicator; Arrow

Cell.accessorytype=uitableviewcellaccessorynone; No arrows

Cell.accessorytype =uitableviewcellaccessorycheckmark; Tick

cell.accessorytype=uitableviewcellaccessorydetailbutton;//Circle +i

*/

/*

Cell.selectionstyle=uitableviewcellselectionstyleblue;//ios7 above default to Gray

Cell.selectionstyle=uitableviewcellselectionstylegray;

cell.selectionstyle=uitableviewcellselectionstylenone;//doesn't show anything.

cell.selectionstyle=uitableviewcellselectionstyledefault;//Default to Gray

Cell.selectedbackgroundview setting the background view but cannot change the width height

*/

/**

Common Properties of TableView

1, RowHeight Line high

2. Separatorcolor Split Line Color

3. Separatorstyle Split Line Style

4, allowmultipleselection whether to allow multi-line selection

*/

/**

Common Properties of cell

1, Selectionstyle

2, Selectedbackgroundview Note that the view width can be changed, but the relative position cannot be changed

3, background note that its backgroundview does not set the frame default is the full line width, even if the change, the width of the high setting is still invalid

*/

/**

Built-in controls for cells

1, Textlabel

2, Detaillabel

3, ImageView

4, Accessorytype

5, Accessoryview

*/

/**

Uitableviewcellstyle

Uitableviewcellstyledefault

UITableViewCellStyleValue1

UITableViewCellStyleValue2

Uitableviewcellstylesubtitle

*/

/**

Accessorytype

Uitableviewcellaccessorydetaildisclosureindicator

Uitableviewcellaccessorycheckwork

*/

/**

Tableview.separatorstyle

Uitableviewcellseparatorstylenone

Uitableviewcellsepatratorstylesingleline

Uitableviewcellseparatorstylesinglelineetched

*/

/**

Set the cell selection style

Uitableviewcellselectionsstylenone

Uitableviewcellselectionsstylegray

Uitableviewcellselectionsstyledefault

Uitableviewcellselectionsstyleblue

*/

2) Notice why Footer/header is added to the section of TableView but not shown

A default section height is generated if the direct control is dragged UITableView.

But if the code is added, you need to set it manually:
tableview.sectionfooterheight=20;

TableView. Sectionheaderheight = 20;

When customizing the Accessoryview, the coordinate values in the frame cannot be changed, only their width and height can be changed.

The height of a wide textfield like switch cannot be modified.

3) There are two ways to set the line height of TableView

A, tableview.rowheight = 5;

b Heightforrowatindexpath (uitableviewdelegate) return nsfloat

The only difference is that, with the execution method, you can get the indexpath that can be changed for different rows to set the row height

Iv. Reuse of cells

1) Understand why you are reusing cells

The cell's browsing mechanism is that when the user slides, the cell that delimits the window is discarded, and then enters a re-created cell, so that once the running time is too long, the constant waste hit will cause the program memory is tight, the efficiency is very low hidden danger is very big. After each cell hour, the cell will be re-created

Cell reuse mechanism: Put the removed cell into the cache pool, reuse it, and just replace the above data

So, when there are many different types of cells that are displayed on the TableView, how do I find them in the cache pool?

Reuse identifiers! The cell name that needs to be reused identifier

2) cell re-operation process

1. Define Reuse Identifiers

2. Find in the cache pool

3. Determine if there is a corresponding cell in the cache pool if not, recreate it.

4. Assigning New data

A

NSString *identifier [email protected] "cell";

B\

UITableViewCell *cell =[tableview Dequeuereusablecellwithidentifier:identifier];

C\

if (cell ==nil) {

Cell =[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier];

D\

Cell.textLabel.text =model.name;

}

V. Nesting of models

1) Import Innermodel in Carmodel

if (self =[super init])

{

[Self setvaluesforkeywithdictionary:dict];

Ning has been reacting interface The Innercarinitwithdictionary method is not defined in the import Innercarmodel

Innercarmodel *innermodel =[[innercarmodel Alloc] innercarinitwithdictionary writing is wrong cause, do not need alloc

Nsmutablearray *muarray =[nsmutablearray Array];

For (Nsdictionary *dict in Self.cars)

{

Innermodel *model =[innermodel Innerinitwithdictionary];

[Muarray Addobject:model];

}

Self.cars =muarray;

return self;

}

2) display in TableView

Number of groups: Self.dataArray.count

Number of rows: Carmodel *model =self.dataarray[section];

return model.cars.count;

Content:

Carmodel *model =self.dataarray[indexpath.section];

Innermodel *innermodel =model.cars[indexpath.row];

Vi. editing of cells, additions and deletions

Objective-c--ui Foundation Development Day Sixth (UITableView)

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.