How to change the height of a table cell

I am trying to read some RSS data. I have data of different sizes. the data is present in a tabke data object. I used a shortcut to add data and resize 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];
    }
}

      

+2


source to share


6 answers


you need to use the following method.

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

      



you need to change the height according to your requirements.

It should be noted that the default height is 44.

+21


source


You need to implement the following method:



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

      

+4


source


- (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 the Interface Builder. Open the * .xib file, select the table, show the "Dimension inspector" and change the line height. But, note that the default height is 44 pixels.

+3


source


There are two ways to set the height UITableViewCell

.:

  • UITableView

    has a property named rowHeight

    , with which you can set UITableViewCell

    the same line height for everyone ;
  • If you implement the protocol UITableViewDelegate

    and include a method tableView:heightForRowAtIndexPath:

    , you can decide the current one UITableViewCell'height

    . In practice, we always use the delegate method to set the cell height only if each cell height can be dynamic! And this method prefers the property UITableView

    rowHeight

    !

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 ;
}

      

+3


source


I've seen a lot of solutions, but everything was wrong or not. You don't need to calculate anything (no font, no size, nothing) ... You can solve all problems with 5 lines in viewDidLoad and autoplay. This is for object 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 fast 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 using xib or as a table in your storyboard With this you don't need to do anything else or override. (Don forget number os lines 0) and lower label (limit) lower "Crawl priority - vertical to 250"

enter image description here

You can download the code in the following url: https://github.com/jposes22/exampleTableCellCustomHeight

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

+1


source


You must know the heights of all cells before calling the delegate tableView:cellForRowAtIndexPath:

, which may require you to store the heights inside your view controller or other list.

The function of setting the heights of the cells tableView:heightForRowAtIndexPath:

. The default height for a table view is 44 pixels for reference.

You can find the documentation here .

0


source







All Articles