-(ID) Init
{
If (Self = [Super initwithstyle: uitableviewstyleplain])
{
Self. Title = @ "Main Menu ";
//-------------------------------------------------------------------------
// Create an array for display
Items _ = [[nsmutablearray alloc] initwithobjects:
@ "Viewcontroller1 ",
@ "Viewcontroller2 ",
Nil];
}
Return self;
}
# Pragma mark ----------- uitableviewdatasource Methods --------
-(Nsinteger) tableview :( uitableview *) tableview
Numberofrowsinsection :( nsinteger) Section
{
Return [items _ count];
}
-(Uitableviewcell *) tableview :( uitableview *) tableview
Cellforrowatindexpath :( nsindexpath *) indexpath
{
// Check whether the Unit has been created
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: @ "Simple-cell"];
If (! Cell)
{
// No new unit is created
Cell = [[[uitableviewcell alloc] initwithframe: cgrectzeroreuseidentifier: @ "Simple-cell"] autorelease];
}
// Set the text string displayed in the unit
Cell. textlabel. Text = [items _ objectatindex: indexpath. Row];
//------------------------
Return cell;
}
# Pragma mark ----- uitableviewdelegate Methods -----------------
-(Void) tableview :( uitableview *) tableview
Didselectrowatindexpath :( nsindexpath *) indexpath
{
Class class = nsclassformstring ([items _ objectatindex: indexpath. Row]);
Id viewcontroller = [[[class alloc] init] autorelease];
If (viewcontroller)
{
[Self. navigationcontroller pushviewcontroller: viewcontroller
Animated: Yes];
}
}
Create an nsarray array. The elements in the nsarray array are displayed in the table. In tableview: cellforrowatindexpath: method, set the elements in nsarray to the table.
Then, the tableview: didselectrowatindexpath: method is called when the table unit is touched (clicked). The indexpath parameter stores the row information that is clicked.