How to increase the height of the cell in a tableview in iphone?

10,134

Solution 1

You can do this in your tableView.delegate/datasource:

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   switch (indexPath.row){
       case 0:
         if(indexPath.section==0)
            return 123.0; // first row is 123pt high
       default:
         return 40.0; // all other rows are 40pt high 
   }
}

This does however slow down scroll performance. Ideally you should set the row height for all rows to be identical using tableview.rowHeight.

Solution 2

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = @"write your dynamic text here";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(320.0f, 999);  // Make changes in width as per your label requirement. 

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return  textSize.height;
}

Solution 3

if you have multiple tableview in one view controller, you can do for each different table view as a below method.

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

    if (tableView == self.tableViewCampaigns) {

        return 45;
    }else if(tableView == self.tableViewAgenda)
        return 32;
    else
        return 29;
}
Share:
10,134
Warrior
Author by

Warrior

I am a software engineer.I have to learn lot in this field.

Updated on June 04, 2022

Comments

  • Warrior
    Warrior almost 2 years

    I am new to iphone development.I am parsing a xml file and displaying the title, date, view and summary in each row of a table.The contents of summar is big ,so only first 3 words are displayed in the cell.How can i increase the height of the row with respect to the length of the contents.All the content should fit properly inside the cell and full content should be displayed.Please help me out.Thanks.

  • Warrior
    Warrior about 14 years
    I want to change the height of only one row and not all the row.
  • Warrior
    Warrior about 14 years
    I want to increase the height of a single row which displays summary
  • Felix Lamouroux
    Felix Lamouroux about 14 years
    The return a different number depending on the NSIndexPath.
  • Warrior
    Warrior about 14 years
    can you elaborate your answer please.since i am new to iphone i am not able to understand it.
  • Warrior
    Warrior about 14 years
    Thanks its working, what should i do, if i use grouped tables and consisting 2 tables.I want to increase the height of the first cell of first table only..
  • Felix Lamouroux
    Felix Lamouroux about 14 years
    I added a check for the first section.