4

I have a tableview with multiple prototype cells that i designed in the storyboard, but i´m stuck with the height problem because my first cell is supose to be different from the second one and so on...I have different identifiers for each cell, and because i designed them in storyboard, i know they´re height´s. I have this in my code, but it´s not working, does anyone knows how to fix it?:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [[UITableViewCell alloc]init];
switch (indexPath.section) 

{
    case 1:

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        return 743.0f; 

        break;

    case 2:

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        return 300.0f;



}

}

Thanks for your time.

Japa
  • 632
  • 7
  • 30

1 Answers1

7

It looks like you are trying to use this method for purposes it wasn't designed for... you'll want to override the method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
        case 1:
           static NSString *CellIdentifier = @"cell1";
           break;
        case 2:
           static NSString *CellIdentifier = @"cell2";
           break;

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

    // Configure the cell...
    return cell;
}

Only change the row height in the heightForRowAtIndexPath:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

switch (indexPath.section) 

{
    case 1:

        return 743.0f; 

        break; //technically never used

    case 2:

        return 300.0f;



}

Check out this tutorial http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 its a good resource

BandoKal
  • 396
  • 1
  • 5
  • 1
    Thank you!!! Bandokal, the problem is finally solved. I also didn´t mentioned, but it was giving me another problem(know solved!), every time i went to editing mode(and i have multiple grouped sections) when i wanted another row(pressing the little green plus button), the content of rows were all messed up..i mean the new row(duplicate of the first section 743.0f) that was created, stood on top of the second section...know everything is ok, thank you again. – Japa Jul 17 '12 at 09:22
  • 1
    Welcome to Stack Overflow (SO) Japa! Since BandoKal answered your question, be sure to click the checkmark next to his answer in order to give him credit for helping you! Also, once you have 10 points of reputation, you can also up vote all of the answers that are helpful by hitting the up arrow, which also gives the person who took the time to help you reputation! – lnafziger Aug 01 '12 at 16:39