iOS Development UI Chapter-uitableview Controls Brief introduction

Source: Internet
Author: User

First, the basic introduction

In the many mobile should use, can see a variety of tabular data.

In iOS, for tabular data presentation, the most common practice is to use Uitableview,uitableview to inherit from Uiscrollview, so vertical scrolling is supported, and performance is excellent.

UITableView have both grouped and ungrouped styles that can be set in storyboard or in code.

Second, data display

UITableView need? A data source (DataSource) to display data
UITableView will query the data source for the total number of rows of data and what data each row displays.

UITableView with no data source set is just an empty shell

Any OC object that complies with the Uitableviewdatasource protocol can be a UITableView data source

The process of presenting the data:

(1) Call the following method of the data source? How many sets of data are there?
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView;

(2) Call the following method of the data source how many rows of data are in each group
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section;

(3) next to the data source? The law learns every?? What to show

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

Third, code example

(1) "junk" code that can be shown basically

  1 #import "NJViewController.h" 2 3 @interface Njviewcontroller () <UITableViewDataSource> 4 @property (weak, no  natomic) Iboutlet UITableView *tableview; 5 6 @end 7 8 @implementation Njviewcontroller 9-(void) viewdidload {[Super viewdidload]; 13//Set The data source of the TableView is self.tableView.dataSource = self; #pragma mark-uitableviewdatasource 19/** 20 * 1. Tell tableview how many groups * * (Nsinteger) numberof Sectionsintableview: (UITableView *) TableView @ NSLog (@ "Numberofsectionsintableview"), return 2; 26} 27/ * * 28 * 2. Number of lines in section group */-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) Secti on {NSLog (@ ' numberofrowsinsection%d ', section), if (0 = = section) {34//No. 0 Group number of rows Turn 2; }else 37 {38//1th group There are many lines of return 3; 40} 41} 42/** 43 * 3. Tell the system what each line shows 44 */45-( UITableViewCell *) TableView: (UitablevieW *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath + NSLog (@ "Cellforrowatindexpath%d%d", INDEXPATH.S Ection, Indexpath.row); //Indexpath.section; Group of Indexpath.row//; Line 50//1. Create cell UITableViewCell *cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefaul T Reuseidentifier:nil]; 52 53//2. Data set//Cell.textLabel.text = @ "Car";  55//Judgment is the first few lines of the group of the IF (0 = = indexpath.section) {//No. 0 Group, if (0 = = Indexpath.row)//No. 0 Group No. 0 line 58 { Cell.textLabel.text = @ "Audi"; }else if (1 = = Indexpath.row)//No. 0 Group 1th Line (Cell.textLabel.text = @ "BMW"; 63} 6             4}else if (1 = = indexpath.section)//1th Group (0 = = Indexpath.row) {//No. 0 Group No. 0 Line 68 Cell.textLabel.text = @ "Honda"; }else if (1 = = Indexpath.row)//No. 0 Group 1th Line {cell.textLabel.text = @ "Toyota";}el Se if (2 = = IndExpath.row)//No. 0 Group 2nd line, Cell.textLabel.text = @ "Mazda"; 75} 76} 77 78//3. Returns the control to display in the return cell; 80 81} 82/** 83 * First section group header show what title * * * * * * (NSString *) TableView: (UITableView *) TableView Titleforhead Erinsection: (Nsinteger) Section (*) ("title"); "(0 = = section) {" "German brand"; 91} Else-------"Japan and Korea brand"; 94} 95} 96/** 97 * What title is displayed at the bottom of section Group 98 * */100-(NSString *) Table  View: (UITableView *) TableView titleforfooterinsection: (Nsinteger) section101 {102 if (0 = = section) {103 return @ "High-end atmospheric grade"; 104}else105 {106 return @ "good"; 107}108}109 @end

Implementation results:

(2) Make the data of the code Independent

Create a new model

1 #import <Foundation/Foundation.h> 2  3 @interface njcargroup:nsobject 4/** 5  *  Title 6  */7 @proper Ty (nonatomic, copy) NSString *title; 8/** 9  *  description  */11 @property (nonatomic, copy) nsstring *desc;12/**13  *  data for all rows in current group  */15 @ Property (Nonatomic, Strong) Nsarray *cars;16 @end
  1 #import "NJViewController.h" 2 #import "NJCarGroup.h" 3 4 @interface Njviewcontroller () <uitableviewdatasource&  Gt  5 @property (weak, nonatomic) Iboutlet UITableView *tableview; 6/** 7 * Save data for all groups (where each element is a model object) 8 */9 @property (nonatomic, strong) Nsarray *cargroups;  Ten @end @implementation Njviewcontroller #pragma mark-lazy load-(Nsarray *) cargroups (*) (_cargroups = = nil)         {20//1. Create model Njcargroup *CG1 = [[Njcargroup alloc] init]; cg1.title = @ "German brand"; 23 Cg1.desc = @ "High-end atmospheric grade"; Cg1.cars = @[@ "Audi", @ "BMW"]; Njcargroup *CG2 = [[Njcargroup alloc] init]; Cg2.title = @ "Japanese and Korean brands"; Cg2.desc = @ "also good"; Cg2.cars = @[@ "Honda", @ "Toyota", @ "Odakyu field"]; Njcargroup *CG3 = [[Njcargroup alloc] init]; Cg3.title = @ "European and American brands"; Cg3.desc = @ "expensive"; Cg3.cars = @[@ "Rolls-Royce" @ "Bugatti", @ "Xiaomi"]; 35//2. Adding a model to an array 36 _cargroups = @[cg1, CG2, CG3]; 37} 38//3. Returns the array of _cargroups return; -(void) viewdidload [[Super Viewdidload]; 45//Set data source for TableView SELF.TABLEVIEW.DATASOURC e = self; #pragma mark-uitableviewdatasource 51/** 52 * 1. Tell TableView how many groups of * * * (Nsinteger) numberof Sectionsintableview: (UITableView *) TableView (NSLog) (@ "Numberofsectionsintableview"); return Self.cargrou Ps.count; 58} 59/** 60 * 2. Section group how many lines * */-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinte GER) Section NSLog (@ "Numberofrowsinsection%d", section), 65//1. Remove the corresponding group model, Njcargroup *g = Self.carg Roups[section]; 67//2. Returns the number of rows in the corresponding group G.cars.count return; 69} 70/** 71 * 3. Tell the system what to show on each line. * */UITableViewCell-(*) TableView: (UITableView *) TableView Cellforrowatindexpath :(Nsindexpath *) Indexpath NSLog (@ "Cellforrowatindexpath%d%d", Indexpath.sectIon, Indexpath.row); //Indexpath.section; Group I//indexpath.row; Line 78//1. Create cell UITableViewCell *cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefaul T Reuseidentifier:nil]; 80 81//2. Set data//Cell.textLabel.text = @ "HI"; 83//2.1 Remove the corresponding group model Njcargroup *g = self.cargroups[indexpath.section]; 85//2.2 Remove data from corresponding row nsstring *name = G.cars[indexpath.row]; 87//2.3 Set the data to be displayed by cell cell.textLabel.text = name; 89//3. Returns the control to display the return cell; 91 92} 93/** 94 * The header of section group shows what title * * * * * * * * * TableView: (UITableView *) TableView Titleforhead Erinsection: (Nsinteger) Section 98 {//Return @ "title"; 100//1. Remove the corresponding group Model 101 Njcargroup *g = self.cargroups[sec tion];102 return g.title;103}104/**105 * section group bottom shows what title 106 *107 */108-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (Nsinteger) section109 {//return @ "title"; 111//1. Remove the corresponding group model Njcargroup *g = self.cargroups[section];113 return g.desc;114}115 @end 

Implementation results:

Tip: Be aware of the benefits of having data independently handled separately as a data model. In addition, what to cut a model, it must be clear.

Iv. Supplementary points

Contentview The default is 3? view

2nd one is Uilabel (accessed via UITableViewCell's Textlabel and Detailtextlabel properties)

3rd one is uiimageview (accessed via UITableViewCell's ImageView property)

UITableViewCell also have a Uitableviewcellstyle attribute, which determines which child views to use Contentview and where those child views are located in Contentview

iOS Development UI Chapter-uitableview Controls Brief introduction

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.