One Simple hero Display program
Njhero. H File code (dictionary to model)
#import <Foundation/Foundation.h> 3 @interface njhero:nsobject 4/** 5 * Avatar 6 */7 @property (Nona Tomic, 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
NJVIEWCONTROLLER.M File Code
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
Implementation results:
Code Note points:
(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
Nsmutablearray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];
(2) Setting the cell height
There are three ways to set the cell's height
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.
2) set in storyboard, suitable for high consistency
3) Use the Proxy method to set the cell height when the cell height of each row is inconsistent
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
{
if (1 = = Indexpath.row) {
return 180;
}
Return 44;
}
Second, some properties of the cell
code example:
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
Implementation results:
Some properties of the cell:
(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);
(2) Set the cell background color, there are two ways to set the cell background color:
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.
Example: Cell.backgroundcolor = [Uicolorbluecolor];
(3) Set the background of the cell default state
Example 1:
UIView *view = [[UIView alloc] init];
View.backgroundcolor = [Uicolor Bluecolor];
Cell.backgroundview = view;
Example 2:
Uiimageview *iv = [[Uiimageviewalloc] initwithimage:[uiimageimagenamed:@ "Buttondelete"];
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)
(4) Set the background of the cell check state
Example:
UIView *view2 = [[UIView alloc] init];
View2.backgroundcolor = [Uicolorpurplecolor];
Cell.selectedbackgroundview = View2;
Third, some properties of TableView
code example:
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
Implementation results:
Some of the properties of TableView:
(1) Set the split style (Tableview.separatorstyle), which is an enumeration type
(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.
Added: Color is divided into 24-bit and 32-bit, as follows
24bit Color
R 8bit 0 ~ 255
G 8bit 0 ~ 255
B 8bit 0 ~ 255
32bit Color
A 8bit 0 ~ 255 (TOU)
R 8bit
G 8bit
B 8bit
#ff FF FF White
#00 00 00 Black
#ff 00 00 Red
#255 00 00
Set as an instance of a custom color: Tableview.separatorcolor = [uicolorcolorwithred:0/255.0green:255/255.0blue:0/255.0alpha:255/255.0];
The received parameter is the proportional value of the color
(3) Set top and bottom view
Tableview.tableheaderview//Top
Tableview.tablefooterview//Bottom
UITableView Controls Basic use