user189464
user189464

Reputation:

How to change the height of table view cell

i m trying to read some RSS data. I have the data that is of different sizes. the data is present in tabke view data object. I used the label to add the data and resize the dat. No success. Please help.

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

    static NSString *CellIdentifier = @"Cell";
    NSLog(@"in the tabel view cell");
    heightOfCell=[self tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Default"];
    if (cell == nil) 
    {
        //cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:@"Default"] autorelease];
        UILabel *label = [[UILabel alloc] init];
        NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
        UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        label.text = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
        CGSize labelSize = [[[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

        label.lineBreakMode=UILineBreakModeWordWrap;
        [label sizeThatFits:labelSize];
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:@"Default"] autorelease];
        //[label sizeToFit];
        [cell addSubview:label];
        [label release];
    }
}

Upvotes: 3

Views: 24651

Answers (6)

ennuikiller
ennuikiller

Reputation: 46985

You need to implement the following method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // some code that compute row's height
}

Upvotes: 4

monjer
monjer

Reputation: 2929

There are two methods to set the UITableViewCell's height .:

  1. UITableView has a property named rowHeight, with which you can use to set all the UITableViewCell to the same row height;
  2. If you implement the UITableViewDelegate protocol , and include tableView:heightForRowAtIndexPath: method, it's you responsbility to decide the current UITableViewCell'height. In practice, we always use the delegate method to set the cell's height, only if each cell's height may be dynamic! And the method is preference to the UITableView's rowHeight property!

So, I think you may prefer the second method:

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

       NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
       UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
       CGSize labelSize = [cellText sizeWithFont:cellFont
                        constrainedToSize:constraintSize
                            lineBreakMode:UILineBreakModeWordWrap];
      return lableSize ;
}

Upvotes: 3

Jose Pose S
Jose Pose S

Reputation: 1295

I saw a lot of solutions but all was wrong or uncomplet. You no need calculate nothing (no font, no size, nothing)... You can solve all problems with 5 lines in viewDidLoad and autolayout. This for objetive C:

_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;

For swift 2.0:

 self.tableView.estimatedRowHeight = 80
 self.tableView.rowHeight = UITableViewAutomaticDimension      
 self.tableView.setNeedsLayout()
 self.tableView.layoutIfNeeded()
 self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

Now create your cell with xib or into tableview in your Storyboard With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"

enter image description here

You can donwload the code in the next url: https://github.com/jposes22/exampleTableCellCustomHeight

References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

Upvotes: 1

darshan
darshan

Reputation: 1115

you need to use the following method.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return 45;
}

you need to change the height as per your requirements

It should be noted that the default height is 44.

Upvotes: 26

Faizan Refai
Faizan Refai

Reputation: 833

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
   // This is just Programatic method you can also do that by xib ! 
}

You can also change it in Interface Builder. Open the *.xib file, select the table, show the Size Inspector, and change the row height. But, please note that the default height is 44 pixels.

Upvotes: 3

Nick Bedford
Nick Bedford

Reputation: 4445

You have to know the heights of all cells before the tableView:cellForRowAtIndexPath: delegate call which may require you to store heights inside your view controller or some other list.

The function to customise the heights of cells is tableView:heightForRowAtIndexPath:. The default height for a table view is 44 pixels, for reference.

You can find the documentation here.

Upvotes: 0

Related Questions