GoldXApp
GoldXApp

Reputation: 2627

Several cells in UITableView

I'm seeking how create several cells to go to different ViewControllers.

For my TableView, I'm using a subclass of UITableViewController.

And when I choose 2 in the following method, I just see 2 identical cells which are doing exactly the same thing. I'm not interested by this. I don't even know their IndexPath in order to change their title.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}

And When I try to put another UITableViewCell in my TableView, it doesn't appear on iOS simulator, even with the same option (same subclass) than my first UITableViewCell which I can see.

Thanks for your help.

Edit : Here is my new code to create 2 cells but doesn't work :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
static NSString *CellIdentifier = @"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[customCell alloc] init];

}

static NSString *CellIdentifier1 = @"Cell1";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil) {
    cell1 = [[customCell alloc] init];

}
// Configure the cell...

return cell;
}

Upvotes: 0

Views: 107

Answers (3)

sergio
sergio

Reputation: 69027

You define your cells in tableView:cellForRowAtIndexPath:, so you should provide an implementation for that method.

tableView:numberOfRowsInSection: only returns the number of cells in the table.

If you need more help, please provide your implementation for tableView:cellForRowAtIndexPath:. This is how a typical implementation looks like:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }

  ... customize your cell ...
}

EDIT:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *CellIdentifier = @"Cell2";
    static NSString *CellIdentifier1 = @"Cell1";

    if(indexPath.row == 0 ) {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
       cell = [[customCell alloc] init];

      }
    } else {

      UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
      if (cell1 == nil) {
        cell1 = [[customCell alloc] init];
      }
    }
    return cell;
}

Upvotes: 1

faterpig
faterpig

Reputation: 344

Use the following:

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section

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

return [delegate numberOfNames];

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.

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath

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.

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];
    }

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.

When the user clicks on any of the rows in the table created, this delegate method will be called

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath

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.

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];
    }

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.

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.

The methods are as follows:

-(NSInteger) numberOfNames
{
    return [myArray count];
}

-(NSString*) nameAtIndex: (NSInteger) index
{
    return [myArray objectAtIndex:index] ;
}

Hope this helps! :)

Upvotes: 0

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

This method gets called when a cell has been selected. You can decide what you wanna do according to the selected row

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
       if (indexPath.row == 0)
           [self goToFirstViewController];
       else
       if(indexPath.row == 1)
        [self goToSecondViewController];
  }

Upvotes: 0

Related Questions