# Import "rootviewcontroller. H"
# Import "person. H"
@ Interface rootviewcontroller () <uitableviewdatasource>
@ Property (nonatomic, retain) nsmutablearray * array;
@ End
@ Implementation rootviewcontroller
-(Void) dealloc
{
[_ Array release];
[Super dealloc];
}
-(Void) viewdidload {
[Super viewdidload];
Self. View. backgroundcolor = [uicolor cyancolor];
// You Need A View Controller to create a navigation controller. Therefore, you must first create a View Controller so that the navigation controller will manage this attempt controller.
// Make the navigation controller the Root View Controller of appdelegate
// Move and edit uitableview
Uitableview * tableview = [[uitableview alloc] initwithframe: Self. View. bounds style :( uitableviewstyleplain)];
Tableview. rowheight = 100; // The Row Height. The default value is 44.
Tableview. backgroundcolor = [uicolor yellowcolor];
Tableview. datasource = self; // specifies a proxy for the proxy to provide data
[Self. View addsubview: tableview];
[Tableview release];
[Self creatdata]; // transfers some code to a method.
}
-(Void) creatdata {
Person * P1 = [person personwithname: @ "" number: @ "123"];
Person * P2 = [person personwithname: @ "raccoon" number: @ "456"];
Person * P3 = [person personwithname: @ "pop-up" number: @ "789"];
Person * P4 = [person personwithname: @ "Skip" number: @ "423"];
Self. array = [nsmutablearray arraywithobjects: P1, P2, P3, P4, nil];
}
-(Void) didreceivememorywarning {
[Super didreceivememorywarning]; // receives a memory warning, passively triggered
}
# Pragma mark-uitableviewdatasource
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {// set the number of rows
Return self. array. count;
}
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {
// Create a cell
// 1. Set the identifier and find the cell
Static nsstring * STR = @ "cell ";
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: Str]; // find the cell in the reuse pool
If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle :( uitableviewcellstylevalue1) reuseidentifier: Str] autorelease]; // direct release is not suitable, so autorelease is used
}
// Assign values to cells
// Cell. textlabel. Text = @ "name ";
// Cell. detailtextlabel. Text = @ "phone ";
Person * person = [self. array objectatindex: indexpath. Row];
Cell. textlabel. Text = person. Name;
Cell. detailtextlabel. Text = person. number;
Return cell;
}
@ End
Uitableview, which is used to assign values to cells by encapsulating classes.