Set a different line height in different sections (storyboard)

I have a tabular view with several prototype cells that I created in a storyboard, but I am stuck with a height problem because my first cell seems to be different from the second, etc. I have different identifiers for each cell, and because I designed them in a storyboard, I know that they are tall. I have this in my code, but it does not work, does anyone know 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; } 

}

Thank you for your time.

+4
source share
1 answer

It looks like you are trying to use this method for purposes that it is not intended for ... you 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; } 

Change row height only in 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

+7
source

All Articles