////VIEWCONTROLLER.M//Simple Table////Created by Jierism on 16/7/20.//copyright©2016 year jierism. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () <UITableViewDelegate,UITableViewDataSource>//declares an array that is used to store the contents of a table cell@property (copy,nonatomic) Nsarray *dwarves;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //Initialize the contents of a table cellSelf.dwarves = @[@"SLEEPY",@"Sneezy",@"bashful",@"HAPPY",@"DOC",@"Grumpy",@"DOPEY",@"Thorin",@"DORIN", @"NORI",@"ORI",@"Balin",@"Dwalin",@"Fili",@"Kili",@"OIN",@"Gloin",@"Bifur",@"Bofur"];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}//Returns the number of elements in the array, that is, the cell row count-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[self.dwarves Count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//declares a static string instance, used as a key, to represent some kind of table cell//more complex tables require different types of table cells to be used depending on their content and location, which requires different table cell identifiers to differentiate each table cell type StaticNSString *simpletableidentifier =@"Simpletableidentifier"; //Generating table CellsUITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:simpletableidentifier]; //When there are no table cells in the view, the number of elements in the array is generated for the table cells if(Cell = =Nil) {Cell= [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault//set the cell's style, which is the defaultReuseidentifier:simpletableidentifier]; } //Insert an image in front of each table cellUIImage *image = [UIImage imagenamed:@"Star"];//this is shown in normal stateCell.imageView.image =image; UIImage*highlightedimage = [UIImage imagenamed:@"star2"];//this is displayed when clickedCell.imageView.highlightedImage =Highlightedimage; Cell.textLabel.text=Self.dwarves[indexpath.row]; Cell.textLabel.font= [Uifont boldsystemfontofsize: -];//Change font Size//sets the detail text for each line, which is displayed when the cell's style is set to non-default if(Indexpath.row <7) {Cell.detailTextLabel.text=@"Mr.disney";//the contents of the previous 7 lines are Mr.disney}Else{Cell.detailTextLabel.text=@"Mr.tolkien";//What's behind is Mr.tolkien. } returncell;}//set Row cell-level indentation-(Nsinteger) TableView: (UITableView *) TableView Indentationlevelforrowatindexpath: (Nsindexpath *) indexpath{returnIndexpath.row%4;}//set line cannot be selected, specify first row here-(Nsindexpath *) TableView: (UITableView *) TableView Willselectrowatindexpath: (Nsindexpath *) indexpath{if(Indexpath.row = =0) { returnNil; }Else{ returnIndexpath; }}//warning Displays- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSString*rowvalue =Self.dwarves[indexpath.row]; NSString*message = [[NSString alloc] Initwithformat:@"You selected%@", Rowvalue]; Uialertcontroller*controller = [Uialertcontroller alertcontrollerwithtitle:@"Row selected!"Message:message Preferredstyle:uialertcontrollerstylealert]; Uialertaction*cancelaction = [Uialertaction actionwithtitle:@"Yes I did"Style:uialertactionstyledefault Handler:nil]; [Controller addaction:cancelaction]; [Self Presentviewcontroller:controller animated:yes completion:nil]; [TableView Deselectrowatindexpath:indexpath animated:yes];}//Modify the height of the row, specifying that all rows are 70r except the first row is 120-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{returnIndexpath.row = =0? -: -;}@endView Code
The above code implements a simple table view that runs as
Select a cell, the icon changes, and a warning pop-up window appears,
One of these codes is displayed only if the cell is set to any style other than default
// sets the detail text for each line, which is displayed when the cell's style is set to non-default if 7 ) { @ "mr.disney"; // the contents of the previous 7 lines are Mr.disney } Else { @ "mr.tolkien"// The content behind is Mr.tolkien } return cell;
Uitableviewcellstylevaluesubtitle Operation Effect:
(PS: In order to see the effect, I changed the font size)
UITableViewCellStyleValue1 Operation Effect:
UITableViewCellStyleValue2 Operation Effect:
Proficient in iOS Development-table View usage