iOS Development UI Chapter-uitableview controls basic use
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>iOS Development UI Chapter-uitableview controls basic use</strong></p></p><p><p><strong>One Simple hero Display program</strong></p></p><p><p>Njhero<strong>.</strong> H file code (dictionary to Model)</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 #import <Foundation/Foundation.h> 2 3 @interface njhero:nsobject 4/** 5 * Avatar 6 * 7 @property ( nonatomic, Copy) NSString *icon; 8/** 9 * name */11 @property (nonatomic, copy) nsstring *name;12/**13 * description */15 @ Property (nonatomic, copy) nsstring *intro;16-(instancetype) initwithdict: (nsdictionary *) dict;18 + (instancetype) Herowithdict: (nsdictionary *) dict;19 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>NJVIEWCONTROLLER.M File Code</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 #import "NJViewController.h" 2 #import "NJHero.h" 3 4 @interface njviewcontroller () <uitableviewdatasource, uitable Viewdelegate> 5/** 6 * Save all Hero Data 7 */8 @property (nonatomic, Strong) nsarray *heros; 9 @property (weak, Nonatomic) iboutlet uitableview *tableview;10 @end12 @implementation NJViewController14 #pragm A mark-lazy load-(nsarray *) heros17 {_heros = = Nil) {19//1. Get the full path of the nsstring *fullpath = [[NS] Bundle mainbundle] pathforresource:@ "heros" oftype:@ "plist"];21//2. More full path loading data, nsarray *dictarray = [NSA Rray arraywithcontentsoffile:fullpath];23//3. dictionary to model Nsmutablearray *models = [nsmutablearray ArrayWithC Apacity:dictarray.count];25 for (nsdictionary *dict in Dictarray) {a njhero *hero = [njhero HEROWITHD ict:dict];27 [models addobject:hero];28}29//4. Assignment Data-_heros = [models copy];31 }32//4. Return Data _heros;34}35-(voId) viewDidLoad37 {"super viewdidload];39//set cell height 40//when The cell height of each row is consistent, use the property to set the height of the cell Self.tablevi Ew.rowheight = 60;42 self.tableView.delegate = self;43}44 #pragma mark-uitableviewdatasource46//return how many Group-(NSI Nteger) numberofsectionsintableview: (uitableview *) tableView48 {return 1;50}51//returns The number of rows per group-(nsinteger) Tablevi Ew: (uitableview *) tableView numberofrowsinsection: (nsinteger) section53 {si return self.heros.count;55}56//returns which group of rows show what Content-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) indexPath58 {59 1. Create CELL60 UITableViewCell *cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseIdenti fier:nil];61//2. Set data 62//2.1 to remove the corresponding row model Njhero *hero = self.heros[indexpath.row];64//2.2 Assignment corresponding data 65 Cell.textLabel.text = hero.name;66 Cell.detailTextLabel.text = hero.intro;67 Cell.imageView.image = [UIImage image named:hero.icon];68//3. return cell69 return cell;70}71 #pragma mark-uitableviewdelegate72/*73//when The height of the cell in each row is inconsistent, use the proxy method to set the height of the Cell-(C Gfloat) tableView: (uitableview *) tableView heightforrowatindexpath: (nsindexpath *) indexPath75 {if (1 = = indexpath.ro W) {180;78}79 return 44;80}81 */82 #pragma mark-controls whether the status bar shows 84/**85 * Returns yes for hidden status bar, no phase Anti-*/87-(BOOL) prefersStatusBarHidden88 {yes;90}91 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Implementation Results:</p></p><p><p></p></p><p><p>Code Note points:</p></p><p><p>(1) in the Code of the dictionary to the model with the following code, the variable array is allocated Dictarray.count storage space, can improve the performance of the program</p></p><p><p>Nsmutablearray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];</p></p><p><p>(2) Setting the cell height</p></p><p><p>There are three ways to set the Cell's height</p></p><p><p>1) can be set in the initial load method, Self.tableView.rowHeight = 60; This applies when the cell height of each row is consistent, using the property to set the Cell's height.</p></p><p><p>2) set in storyboard, suitable for high consistency</p></p><p><p>3) use the Proxy method to set the cell height when the cell height of each row is inconsistent</p></p><p><p>-(cgfloat) tableView: (uitableview *) tableView heightforrowatindexpath: (nsindexpath *) Indexpath</p></p><p><p>{</p></p><p><p>if (1 = = Indexpath.row) {</p></p><p><p>Return 180;</p></p><p><p>}</p></p><p><p>Return 44;</p></p><p><p>}</p></p><p><p>second, Some properties of the cell</p></p><p><p>Code example:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 #import "NJViewController.h" 2 #import "NJHero.h" 3 4 @interface njviewcontroller () <uitableviewdatasource, UIT Ableviewdelegate> 5/** 6 * Save all Hero Data 7 */8 @property (nonatomic, Strong) nsarray *heros; 9 @property (weak, Nonatomic) iboutlet uitableview *tableview; Ten @end @implementation njviewcontroller #pragma mark-lazy load-(nsarray *) heros (_heros = Nil) {19//1. Get full path nsstring *fullpath = [[nsbundle mainbundle] pathforresource:@ "heros" oftype:@ "plist"]; 21//2. more full path loading data nsarray *dictarray = [nsarray arraywithcontentsoffile:fullpath]; 23//3. dictionary to model Nsmutablearray *models = [nsmutablearray arrayWithCapacity:dictArray.count]; (nsdictionary *dict in dictarray) {njhero *hero = [njhero herowithdict:dict]; 27 [models addobject:hero]; 28} 29//4. Assignment Data _heros = [models copy]; 31} 32//4. Return Data 33 Return _heros; (void) viewdidload PNS {[super viewdidload]; 39//set cell height 40//when The cell height of each row is consistent, use the property to set the Cell's Height self.tableView.rowHeight = 60; Self.tableView.delegate = self; #pragma mark-uitableviewdatasource 47//return How many groups-(nsinteger) numberofsectionsintableview: (uitableview *) TableView {1, 51} 52//returns The number of rows in each group-(nsinteger) tableView: (uitableview *) tableView numberofrowsins Ection: (nsinteger) Section [self.heros.count]; 56} 57//returns which row of which group is displayed what content (uitableviewcell *) Tablevie W: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) indexpath 59 {60//1. Create Cell UITableViewCell * Cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:nil]; 62//2. set Data 63//2.1 take out the corresponding line model Njhero *hero = self.heros[indexpath.row]; 65//2.2 assignment corresponds to the data of the Cell.textLabel.text = hero.name; Cell.detailTextLabel.text = Hero.intro; Cell.imageView.image = [UIImage imageNamed:hero.icon]; 69 70//2.3 Set Auxiliary view of cell Cell.accessorytype = uitableviewcellaccessorydisclosureindicator; if (0 = = Indexpath.row) {cell.accessoryview = [UIButton buttonwithtype:uibuttontypecontactadd]; 74 }else {cell.accessoryview = [[uiswitch alloc] init]; +//UIButton *btn = [[UIButton alloc ] init]; //btn.backgroundcolor = [uicolor redcolor]; //cell.accessoryview = btn; 81 82 83//2.4 Set the background color of the cell Cell.backgroundcolor = [uicolor bluecolor]; 85 86//set The default state of the background UIView//*view = [[UIView alloc] init]; //view.backgroundcolor = [uicolor bluecolor]; //cell.backgroundview = view; Uiimageview *iv = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@ "buttondelete"]; Cell.backgroundview = iv; 93 94//set The background of the selected state UIView *view2 = [[UIView alloc] init]; View2.bacKgroundcolor = [uicolor purplecolor]; Cell.selectedbackgroundview = view2; 98//3. return cell cell;100}101 102 103 #pragma mark-controls whether the status bar displays 104/**105 * Returns yes for hidden status bar, no opposite 106 */10 7-(BOOL) prefersStatusBarHidden108 {109 return yes;110}111 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Implementation Results:</p></p><p><p></p></p><p><p>Some properties of the Cell:</p></p><p><p>(1) Set the auxiliary view of the cell, set the Cell.accessoryview (the system provides an enumeration type, you can also customize the @ parent class pointer to the subclass object);</p></p><p><p>(2) set the cell background color, There are two ways to set the cell background color:</p></p><p><p>The background of the cell can be set through both BackgroundColor and BACKGROUNDVIEW. But Backgroundview has a higher priority than backgroundcolor, so if BackgroundColor and Backgroundview are set at the same time, Then Backgroundview will cover Backgroundcolor.</p></p><p><p>Example: Cell.backgroundcolor = [uicolorbluecolor];</p></p><p><p>(3) set the background of the cell default state</p></p><p><p>Example 1:</p></p><p><p>UIView *view = [[UIView alloc] init];</p></p><p><p>View.backgroundcolor = [uicolor bluecolor];</p></p><p><p>Cell.backgroundview = view;</p></p><p><p>Example 2:</p></p><p><p>Uiimageview *iv = [[uiimageviewalloc] initwithimage:[uiimageimagenamed:@ "buttondelete"];</p></p><p><p>Cell.backgroundview = iv; (the parent pointer points to the subclass object, you can use the picture to set a gorgeous effect with a simple action)</p></p><p><p>(4) set the background of the cell check state</p></p><p><p>Example:</p></p><p><p>UIView *view2 = [[UIView alloc] init];</p></p><p><p>View2.backgroundcolor = [uicolorpurplecolor];</p></p><p><p>Cell.selectedbackgroundview = view2;</p></p><p><p>third, Some properties of TableView</p></p><p><p>Code example:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 #import "NJViewController.h" 2 3 @interface njviewcontroller () <UITableViewDataSource> 4 5 @end 6 7 @implement ation Njviewcontroller 8 9-(void) viewDidLoad10 {one [super viewdidload];12 13//1. creating Tableview14 UITableView *tableview = [[uitableview alloc] init];15 tableview.frame = self.view.bounds;16 17//2. Set the data source at Tableview.da Tasource =self;19 20//3. Add TableView to view21 [self.view addsubview:tableview];22 23//4. set split line style 24// Tableview.separatorstyle = uitableviewcellseparatorstylenone;25 26//5. Set Split Line Color 27 The received parameter is the proportional value of the Color. tableview.se Paratorcolor = [uicolor colorwithred:0/255.0 green:255/255.0 blue:0/255.0 alpha:255/255.0];29 30//set tableview Head View Tableview.tableheaderview = [UIButton buttonwithtype:uibuttontypecontactadd];32 Tableview.tablefooterview = [ [uiswitch alloc] init];33}34-(nsinteger) numberofsectionsintableview: (uitableview *) tableView36 {notoginseng return 1;38} -(nsinteger) TablevieW: (uitableview *) tableView numberofrowsinsection: (nsinteger) section40 {return 10;42}43-(uitableviewcell *) tab Leview: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) indexPath45 {46//1. Create cell47 UITableViewCell *cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:nil];48 49//2. Set data for cell 5 0 Cell.textLabel.text = [nsstring stringwithformat:@ "%d", indexpath.row];51 52//3. return to cell53 return cell;54} -(BOOL) prefersStatusBarHidden57 {yes;59}60 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Implementation Results:</p></p><p><p></p></p><p><p>Some of the properties of Tableview:</p></p><p><p>(1) set the split style (tableview.separatorstyle), which is an enumeration type</p></p><p><p>(2) set the color of the split line, you can directly use the system given the color, if the system given the color can not meet the requirements, but also customizable.</p></p><p><p>Added: color is divided into 24-bit and 32-bit, as follows</p></p><p><p>24bit Color</p></p><p><p>R 8bit 0 ~ 255</p></p><p><p>G 8bit 0 ~ 255</p></p><p><p>B 8bit 0 ~ 255</p></p><p><p></p></p><p><p>32bit Color</p></p><p><p>A 8bit 0 ~ 255 (tou)</p></p><p><p>R 8bit</p></p><p><p>G 8bit</p></p><p><p>B 8bit</p></p><p><p></p></p><p><p>#ff FF FF White</p></p><p><p>#00 00 00 Black</p></p><p><p>#ff 00 00 Red</p></p><p><p>#255 00 00</p></p><p><p></p></p><p><p>Set as an instance of a custom color: Tableview.separatorcolor = [uicolorcolorwithred:0/255.0green:255/255.0blue:0/255.0alpha:255/255.0];</p></p><p><p>The received parameter is the proportional value of the color</p></p><p><p>(3) set top and bottom view</p></p><p><p>Tableview.tableheaderview//top</p></p><p><p>Tableview.tablefooterview//bottom</p></p><p><p>iOS Development UI Chapter-uitableview controls basic use</p></p></span>
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