The previous two blogs simply learned the use of the Picker view control and then learned a very important view of the iOS app-the table view.
In the table view, each table view is an instance of UITableView, and each visible row is an instance of UITableViewCell.
Table views have two basic formats, grouped tables and plain tables, and ordinary tables can be indexed, and tables that implement the index become index tables. (PS. Technically, the grouping table can also be indexed, but it seems that Apple's design specification does not support)
A simple table View application interface design:
Drag a table view control into the storyboard, he will fill the screen automatically, as to constraints, you can create their own, remember to set the tag of the TableView is 1, will be used later.
The Outlets in table view will have the DataSource and delegate two settings, press and hold the empty circle behind each set item, and drag to the Controller button in the storyboard to complete the association.
Code Explanation:
The first step is to implement the Uitableviewdatasource and uitableviewdelegate two protocols in the corresponding controller's. h file.
In the. m file, first define a property of type Nsarray, which is used to hold the data tableview to display, and to assign a value to Nsarray in the Viewdidload method.
In the Viewdidload method also need to do is to tableview the top of the bottom offset a certain number to beautify the interface (not to beautify, if not to do this step, TableView top will be next to the status bar, it is difficult to see < ( ̄▽ ̄) > wow haha ...)
uitableview* TableView = (ID) [self.view viewwithtag:1];//get control via tag = Tableview.contentinset; - ; [TableView Setcontentinset:contentinset];
Then implement two delegate methods, one is-(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (nsinteger) section, Returns the number of rows for this table,
The other is-(uitableviewcell*) TableView: (uitableview*) TableView Cellforrowatindexpath: (nsindexpath*) Indexpath, This method returns the Tableviewcell of the specified row.
First look at the parameters of this method, the first parameter is a TableView, which indicates the table triggering this method. The second parameter is a value of type Nsindexpath, which has two main attributes, section and row, group, and Row.
Creates a UITableViewCell object, used here by the Dequeuereusablecellwithidentifier: method, which returns a cell that can be reused, and possibly returns nil
So we have to judge the return value, and if the return value is nil, you need to instantiate a cell
With the cell, you can set the cell to display the text
Cell.textLabel.text = Self.dwarves[indexpath.row];
Full code:
1 //2 //VIEWCONTROLLER.M3 //Simple Table4 //5 //Created by Zhang Guang in 15/10/19.6 //Copyright (c) Zhang Guang, 2015. All rights reserved.7 //8 9 #import "ViewController.h"Ten One @interfaceViewcontroller () A@property (Strong, nonatomic) nsarray*dwarves; - @end - the @implementationViewcontroller - -- (void) Viewdidload - { + [Super Viewdidload]; -Self.dwarves = @[@"Sleepy",@"Sneezy",@"bashful",@"Happy",@"Doc",@"Grumpy",@"Dopey",@"Thorin",@"Dorin",@"Nori",@"Ori",@"Balin",@"Dwalin",@"Fili",@"Kili",@"Oin",@"Gloin",@"Bifur",@"Bofur",@"Bombur" ]; +uitableview* TableView = (ID) [Self.view Viewwithtag:1]; AUiedgeinsets Contentinset =Tableview.contentinset; atContentinset.top = -; - [TableView Setcontentinset:contentinset]; - } - --(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (nsinteger) section - { in return[self.dwarves Count]; - } to +-(uitableviewcell*) TableView: (uitableview*) TableView Cellforrowatindexpath: (nsindexpath*) Indexpath - { the Staticnsstring* Simpletableindetifier =@"Simpletableidentfier"; *uitableviewcell* cell =[TableView Dequeuereusablecellwithidentifier:simpletableindetifier]; $ if(Cell = =Nil) {Panax NotoginsengCell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableindetifier]; - } theCell.textLabel.text =Self.dwarves[indexpath.row]; + returncell; A } the + @endView Code
The table view control exercise in iOS