Appdelegate. m
MainViewController *mainCtrl = [[MainViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:mainCtrl]; self.window.rootViewController = navCtrl; [mainCtrl release]; [navCtrl release];
Mainviewcontroller. m
# Import "mainviewcontroller. H "# import" firstviewcontroller. H "# import" secondviewcontroller. H "# import" thirdviewcontroller. H "@ interface mainviewcontroller () @ end @ implementation mainviewcontroller-(ID) initwithstyle :( uitableviewstyle) style {self = [Super initwithstyle: style]; If (Self) {self. title = @ "cell customization method";} return self;}-(void) viewdidload {[Super viewdidload]; // sets whether the selected view exists after it is returned, the default value is yes // self. clearsselectiononviewwillappear = no;}-(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} # pragma mark-Table view Data Source-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {return 1;}-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) section {return 3;}-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * iden = @ "cell_root "; uitableviewcell * cell = [tableview metadata: iden]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: iden];} cell. textlabel. TEXT = [nsstring stringwithformat: @ "cell customization method in % d", indexpath. row + 1]; return cell;} // click the corresponding protocol method of the cell-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {// click the event if (indexpath. row = 0) {firstviewcontroller * firstctrl = [[firstviewcontroller alloc] init]; [self. navigationcontroller pushviewcontroller: firstctrl animated: Yes]; [firstctrl release];} else if (indexpath. row = 1) {secondviewcontroller * secondctrl = [[secondviewcontroller alloc] init]; [self. navigationcontroller pushviewcontroller: secondctrl animated: Yes]; [secondctrl release];} else if (indexpath. row = 2) {thirdviewcontroller * thirctrl = [[thirdviewcontroller alloc] init]; [self. navigationcontroller pushviewcontroller: thirctrl animated: Yes]; [thirctrl release] ;}} @ end
---------------------------- The first method [built-in system] ---------------------------------
Firstviewcontroller. h
@interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>@property(nonatomic, retain)NSArray *data;
Firstviewcontroller. m
-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) attributes {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {self. title = @ "first cell customization method";} return self;}-(void) viewdidload {[Super viewdidload]; // create table view uitableview * tableview = [[uitableview alloc] initwithframe: cgrectmake (0, 20,320,460) style: uitableviewstyleplain]; tableview. delegate = self; tableview. datasource = self; [self. view addsubview: tableview]; [tableview release]; // read the nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "news" oftype: @ "plist"]; _ DATA = [[nsarray alloc] initwithcontentsoffile: path] ;}# Pragma mark-uitableview datasource-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return _ data. count;}-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * iden = @ "cell_first"; uitableviewcell * cell = [tableview preview: iden]; If (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: iden] autorelease]; // Add a subview to the cell. // 1. create a label uilabel * titlelabel = [[uilabel alloc] initwithframe: cgrectmake (10, 5,300, 20)]; titlelabel. tag= 101; titlelabel. backgroundcolor = [uicolor clearcolor]; titlelabel. font = [uifont boldsystemfontofsize: 15]; [cell. contentview addsubview: titlelabel]; [titlelabel release]; // 2. create a label uilabel * commentlabel = [[uilabel alloc] initwithframe: cgrectmake (10, 25,150, 20)]; commentlabel. font = [uifont systemfontofsize: 13]; commentlabel. tag = 102; commentlabel. textcolor = [uicolor graycolor]; commentlabel. backgroundcolor = [uicolor clearcolor]; [cell. contentview addsubview: commentlabel]; [commentlabel release]; // 3. create a label uilabel * timelabel = [[uilabel alloc] initwithframe: cgrectmake (210, 25,100, 20)]; timelabel. font = [uifont systemfontofsize: 13]; timelabel. tag= 103; timelabel. textcolor = [uicolor graycolor]; timelabel. textalignment = nstextalignmentright; timelabel. backgroundcolor = [uicolor clearcolor]; [cell. contentview addsubview: timelabel]; [timelabel release];} // Add data nsdictionary * DIC = [_ DATA objectatindex: indexpath. row]; // comment time nsstring * time = [DIC objectforkey: @ "time"]; // comment count nsstring * COUNT = [DIC objectforkey: @ "commentcount"]; // Title nsstring * Title = [DIC objectforkey: @ "title"]; uilabel * titlelabel = (uilabel *) [cell. contentview viewwithtagag: 101]; titlelabel. TEXT = title; uilabel * commentlabel = (uilabel *) [cell. contentview viewwithtag: 102]; commentlabel. TEXT = [nsstring stringwithformat: @ "% @ comment count", Count]; uilabel * timelabel = (uilabel *) [cell. contentview viewwithtagag: 103]; timelabel. TEXT = [nsstring stringwithformat: @ "% @ hour ago", time]; return cell ;}
--------------------------- Method 2 [NIB file] ---------------------------------
Secondviewcontroller. h
@interface SecondViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>@property(nonatomic, retain)NSArray *data;
Secondviewcontroller. m
# Import "secondviewcontroller. H "@ interface secondviewcontroller () @ end @ implementation secondviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {self. title = @ "cell second customization method";} return self;}-(void) viewdidload {[Super viewdidload]; // create table view uitableview * tableview = [[uitableview alloc] initwithframe: cgrectmake (0, 20,320,460) style: uitableviewstyleplain]; tableview. delegate = self; tableview. datasource = self; [self. view addsubview: tableview]; [tableview release]; // read the nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "news" oftype: @ "plist"]; _ DATA = [[nsarray alloc] initwithcontentsoffile: path];}-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return _ data. count;}-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * iden = @ "cell_second"; uitableviewcell * cell = [tableview progress: iden]; If (cell = nil) {Cell = [[[nsbundle mainbundle] loadnibnamed: @ "cell" Owner: Self options: Nil] lastobject];} // Add data nsdictionary * DIC = [_ DATA objectatindex: indexpath. row]; // comment time nsstring * time = [DIC objectforkey: @ "time"]; // comment count nsstring * COUNT = [DIC objectforkey: @ "commentcount"]; // Title nsstring * Title = [DIC objectforkey: @ "title"]; uilabel * titlelabel = (uilabel *) [cell. contentview viewwithtagag: 101]; titlelabel. TEXT = title; uilabel * commentlabel = (uilabel *) [cell. contentview viewwithtag: 102]; commentlabel. TEXT = [nsstring stringwithformat: @ "% @ comment count", Count]; uilabel * timelabel = (uilabel *) [cell. contentview viewwithtagag: 103]; timelabel. TEXT = [nsstring stringwithformat: @ "% @ hour ago", time]; return cell;}/* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender {// get the New View Controller Using [segue destinationviewcontroller]. // pass the selected object to the New View Controller .} * // @ end
---------------------------------------- [Custom cell] ------------------------------------
Thirdviewcontroller. h
@interface ThirdViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>@property(nonatomic, retain)NSMutableArray *data;
Thirdviewcontroller. m
# Import "thirdviewcontroller. H "# import" newsmodel. H "# import" newscell. H "@ interface thirdviewcontroller () @ end @ implementation thirdviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {self. title = @ "cell third customization method";} return self;}-(void) viewdidload {[Super viewdidload]; // load data nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "news" oftype: @ "plist"]; nsarray * arrary = [[nsarray alloc] initwithcontentsoffile: path]; _ DATA = [[nsmutablearray alloc] init]; // submit the data in the dictionary to the news object for (nsdictionary * DIC in arrary) {newsmodel * model = [[newsmodel alloc] init]; // nsstring * Title = [DIC objectforkey: @ "title"]; // model. title = title; model. title = [DIC objectforkey: @ "title"]; model. commentcount = [DIC objectforkey: @ "commentcount"]; model. time = [DIC objectforkey: @ "time"]; // store the model object in the array [_ DATA addobject: Model]; [model release];} // create table view uitableview * tableview = [[uitableview alloc] initwithframe: cgrectmake (0, 20,320,460) style: uitableviewstyleplain]; tableview. delegate = self; tableview. datasource = self; [self. view addsubview: tableview] ;}# Pragma mark-uitableview datasource-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return _ data. count;}-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * iden = @ "cell_three"; newscell * cell = [tableview details: iden]; If (cell = nil) {Cell = [[[newscell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: iden] autorelease];} cell. model = [_ DATA objectatindex: indexpath. row]; return cell;} @ end
Newscell. h
@class NewsModel;@interface NewsCell : UITableViewCell { UILabel *_titleLabel; UILabel *_commentLabel; UILabel *_timeLablel; }@property(nonatomic, retain)NewsModel *model;
Newscell. m
# Import "newscell. H "# import" newsmodel. H "@ implementation newscell-(ID) initwithstyle :( mandatory) style reuseidentifier :( nsstring *) reuseidentifier {self = [Super initwithstyle: style reuseidentifier: reuseidentifier]; If (Self) {// initialization code [self _ initview];} return self;}-(void) _ initview {// create a label with the title/_ titlelabel = [[uilabel alloc] initwithframe: cgrectmake (10, 5,300, 20)]; _ titlelabel = [[uilabel alloc] init]; _ titlelabel. backgroundcolor = [uicolor clearcolor]; _ titlelabel. font = [uifont boldsystemfontofsize: 16]; [self. contentview addsubview: _ titlelabel]; // create a label that displays the number of comments/_ commentlabel = [[uilabel alloc] initwithframe: cgrectmake (10, 25,150, 20)]; _ commentlabel = [[uilabel alloc] init]; _ commentlabel. backgroundcolor = [uicolor clearcolor]; _ commentlabel. textcolor = [uicolor graycolor]; _ commentlabel. font = [uifont boldsystemfontofsize: 13]; [self. contentview addsubview: _ commentlabel]; // create a label that displays the publication time. // _ timelablel = [[uilabel alloc] initwithframe: cgrectmake (200, 25,100, 20)]; _ timelablel = [[uilabel alloc] init]; _ timelablel. backgroundcolor = [uicolor clearcolor]; _ timelablel. textcolor = [uicolor graycolor]; _ timelablel. font = [uifont systemfontofsize: 13]; [self. contentview addsubview: _ timelablel];}/* call * // * 1 when the view is to be displayed. layout, set frame 2. add data */-(void) layoutsubviews {[Super layoutsubviews]; // layout _ titlelabel. frame = cgrectmake (10, 5,300, 20); _ commentlabel. frame = cgrectmake (10, 25,150, 20); _ timelablel. frame = cgrectmake (200, 25,100, 20); // Add data _ titlelabel. TEXT = _ model. title; _ commentlabel. TEXT = [nsstring stringwithformat: @ "number of comments % @", _ model. commentcount]; _ timelablel. TEXT = [nsstring stringwithformat: @ "% @ hour ago", _ model. time] ;}@ end
Newsmodel. h
@ Interface newsmodel: nsobject @ property (nonatomic, copy) nsstring * Title; // Title @ property (nonatomic, copy) nsstring * commentcount; // number of comments @ property (nonatomic, copy) nsstring * time; // time
Newsmodel. m
- (void)dealloc{ [_commentCount release]; [_title release]; [_time release]; [super dealloc];}
Three Cell customization methods