Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the following:</p> <pre><code>- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section </code></pre> <p>This delegate method returns the number of rows you want in that particular section. So if you want more than 2 rows, or you want the number of rows to be dynamic, you can create a NSArray in the AppDelegate or in the init method of the viewController class, and return the number in the numberOfRowsInSection method like </p> <pre><code>return [delegate numberOfNames]; </code></pre> <p>In my example above, I created an array in my AppDelegate and also a method to return the number of objects I have in that array so that I can create the number of rows for my table.</p> <pre><code>- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath </code></pre> <p>This delegate method will show what you want to display in each cell. Therefore, following on from my array created in my AppDelegate, I first create the cell, then I will set the text I want to display on the cell with a method I created in my AppDelegate that will return a NSString while taking in a NSInteger so that I can loop through my array and display the text accordingly.</p> <pre><code>static NSString* MyIdentifier = @"Default"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if( cell == nil ) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; cell.textLabel.text = [delegate nameAtIndex:indexPath.row]; } </code></pre> <p>nameAtIndex is the name of the method I created in my AppDelegate that will return the NSString object at the specific index (ie. the row number) from the NSArray I created to store all the items of my table.</p> <p>When the user clicks on any of the rows in the table created, this delegate method will be called</p> <pre><code>- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath </code></pre> <p>And in here, I will check if the text displayed matches any of the items in my array from the AppDelegate that stores the items in the table, and create the view that is necessary.</p> <pre><code>UIViewController* viewController = nil ; NSString* nameInArray = [delegate nameAtIndex:indexPath.row] ; if( [nameInArray isEqualToString:@"firstName"] ) { viewController = [[FirstViewController alloc] init]; } else if( [nameInArray isEqualToString:@"secondName"] ) { viewController = [[SecondViewController alloc] init]; } else if( [nameInArray isEqualToString:@"thirdName"] ) { viewController = [[ThirdViewController alloc] init]; } </code></pre> <p>So with these 3 delegate methods, you will be able to create the table using a NSArray created, and be able to redirect the user to a viewController according to which option in the table he chooses. You will not have to keep editing the delegate methods if you choose to add more rows to the table as well since you are returning the count of the array when setting up the table.</p> <p>The array and methods to get the data of the array can be created in the viewController as well, not necessarily in the AppDelegate, in case you were wondering. </p> <p>The methods are as follows:</p> <pre><code>-(NSInteger) numberOfNames { return [myArray count]; } -(NSString*) nameAtIndex: (NSInteger) index { return [myArray objectAtIndex:index] ; } </code></pre> <p>Hope this helps! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload