UITableView Introduction
- UITableView is a UI control that is used to display data in the form of a list
- Example
- QQ Friends List
- Contacts
- iphone Settings list
TableView Common Properties
Set the height of each row of cellsSelf. TableView. RowHeight =100;Set the height of each group of headersSelf. TableView. Sectionheaderheight =50;Set the height of each set of tailsSelf.tableView.sectionFooterHeight = 50;//Set split Line Color self. TableView. Separatorcolor = [uicolor redcolor]; //Set split line style self. TableView. Separatorstyle = Uitableviewcellseparatorstylenone; //Set header control self . TableView. Tableheaderview = [[Uiswitch alloc] init]; //Set footer control self . TableView. Tablefooterview = [UIButton buttonwithtype:uibuttontypecontactadd ];
Two styles of UITableView
只读属性
- Read-only property, cannot be modified in code Uitableviewstyleplain
- A group showing section = 1;
uitableviewstylegrouped
Show Data Compliance Protocol
Set up a data source
Implementing a Data source method
- How many groups to tune first
//调用数据源的下面方法得知一共有多少组数据- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//调用数据源的下面方法得知每一组有多少行数据- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- 每行数据
//调用数据源的下面方法得知每一行显示什么内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableView to the right of the index bar
Sets the color of the index text to the right of TableViewSelf. TableView. Sectionindexcolor = [uicolor redcolor]; //Set the color of the right index text background self . TableView. Sectionindexbackgroundcolor = [uicolor graycolor]; //Data source Method index information-(Nsarray *) Sectionindextitlesfortableview: (uitableview *) tableview{ //Returns an array, The elements in the array are display information, only hints, and results are grouped by index location return [nsarray arraywithobjects:@ "A",@ "B",@ "C", @ "D", nil];}
TableView Common methods
Set header data for grouping-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{Return@ "Head";}Sets the trailing data for the group-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (Nsinteger) section{Return@ "Head";} * * Call when a line is selected (click) */-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Xmgwine *wine = Self.winearray[indexpath.row];NSLog (@ "clicked:%@", wine.name);NSLog (@ "checked:%zd", Indexpath. row);}/** * Call */-when a row is unchecked (void) TableView: (uitableview *) TableView Diddeselectrowatindexpath: (nsindexpath *) indexpath{ NSLog ( @ "Unchecked:%zd", Indexpath. row);} /** * Returns the height of each cell */-(cgfloat) TableView: (uitableview *) TableView Heightforrowatindexpath: ( Nsindexpath *) indexpath{ if (indexpath. Row% 2 = = 0) { return 50;} else { return ;}}
UITableViewCell
- Cell's style properties
- Uitableviewcellstyledefault
- UITableViewCellStyleValue1
- UITableViewCellStyleValue2
- Uitableviewcellstylesubtitle
The cell right indicates the style's properties Accessoryview
The cell right indicates the style's properties Accessorytype
- Uitableviewcellaccessorynone
- Uitableviewcellaccessorydisclosureindicator
- Uitableviewcellaccessorydetaildisclosurebutton
- Uitableviewcellaccessorycheckmark
- Uitableviewcellaccessorydetailbutton
Cell is clicked on the color changeiOS 7 之前的
- Uitableviewcellselectionstylenone
- Uitableviewcellselectionstyleblue
- Uitableviewcellselectionstylegray
- Uitableviewcellselectionstyledefault
Create cell performance analysis and optimize cell reuse principles
iOS devices have limited memory, and if you use UITableView to display thousands of data, you need thousands of UITableViewCell objects, which will deplete the memory of your iOS device. To work around this problem, you need to reuse the UITableViewCell object
- 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 row is not necessarily the same uitableviewcell, So a uitableview may have different types of uitableviewcell, and there will be many different types of UITableViewCell in the object pool, Then UITableView may get the wrong type when reusing UITableViewCell UITableViewCell
- Solution: UITableViewCell has a nsstring *reuseidentifier property that allows you to set reuseidentifier by passing in a specific string identifier when initializing UITableViewCell ( Generally used UITableViewCell class name). 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
When the memory analysis is not optimized when not optimized
- Destroy when you leave the visual range
- Frequently open memory, destroy memory
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc] init]; cell.textLabel.text = [NSString stringWithFormat:@"%zd行",indexPath.row]; NSLog(@"cellForRowIndexPath --%zd",indexPath.row); return cell;}
Optimized memory address analysis
Reuse code for cell
- (UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{1. Define the identity of a cellStatic defines the variable----only the initial changestatic nsstring *id = @ "Jrcell"; Span class= "Hljs-comment" >//2. Remove the cell uitableviewcell *cell = [TableView Dequeuereusablecellwithidentifier:id]; //3. If the cache pool does not have cell if (cell = = nil) {cell = [[uitableviewcell Alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; //try to set the cell initialization, put it in this code block //if this setting is all cells must be consistent, Can be placed in this code block Cell.textlabel.font = [ uifont systemfontofsize:30];} //4. Set the properties of the cell ... return cell;
Reuse code for cell
新写法
The UITableView of Ios-ui control (i.)