Table view October 4

Source: Internet
Author: User

-(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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.