Shubham Sharma
Shubham Sharma

Reputation: 529

UITableViewCell row height

Is there any such method or solution to auto adjust row height of tableview depending on content.I mean that I don't want to specify row height and want row height according to content of the row.If there is no such method then tell me the solution that how can I change height when the orientation of the device changes ?

Upvotes: 3

Views: 16964

Answers (6)

Mnsd
Mnsd

Reputation: 35

func tableView(_ heightForRowAttableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension }

for dynamic height for row otherwise you can give static height instead of "UITableViewAutomaticDimension"

Upvotes: 0

user3614525
user3614525

Reputation: 21

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

    switch (indexPath.row){
        case 0:
            if(indexPath.section == 0)
                //title 
                return  75;
        default:
            return 75;
    }
}

Upvotes: 0

LJ Wilson
LJ Wilson

Reputation: 14427

You HAVE to use:

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

Yea - I know you know this method very well, and I have read your question carefully.

  1. You have to use that delegate method to dynamically control the height of the rows. My suggestion is to examine the content at each cell from your backend data model for the tableview and return a number that is appropriate for that cell something like:

NSDictionary *thisCell = (NSDictionary *)[myArray objectAtIndexPath:indexPath.row];

NSString *myCellContents = [thisCell valueForKey:@"MyStringIWantToCheckOut"];

if ([mycellContents length] > 25) {        
    return 80; 
} else {        
    return 40;
}
  1. As far as when orientation changes, you would have to do that in the delegate method:

    -(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

and then fire reloadData on the TableView.

Upvotes: 0

Sharanya K M
Sharanya K M

Reputation: 1805

check this out

// --dynamic cell height according to the text--
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

  NSString *text = <your text>;
  CGSize constraint = CGSizeMake(210, 20000.0f); 
  CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Helvetica-Light" size:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
  // constratins the size of the table row according to the text

  CGFloat height = MAX(size.height,60);

  return height + (15);
  // return the height of the particular row in the table view
}

hope this helps

EDIT for the orientation have you tried this method?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations

    return YES;
}

or else if your table cells are custom with labels and imageviews etc then you can use setAutoresizingMask: on each to auto adjust to orientations.

[yourview setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

Upvotes: 2

spider1983
spider1983

Reputation: 1098

You will have to find the the height of the your content and can use it

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     return "your content height + your padding height value";
 }

On orientation change just reload your table view thats it;

Upvotes: 4

NikosM
NikosM

Reputation: 1131

You can implement the - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

delegate method of UITableView.

You should be able to find plenty of tutorials about it if you google it.

Here's one for example:

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

Upvotes: 1

Related Questions