1

I am Creating One tableview with tableview cell fro XIB. When I integrate tableview cell with Tableview and run it it is not showing perfectly ,It shows me override.

Image is

enter image description here

Code is

static NSString *cellIdentifier =@"DiscussionTableViewCell";

            DiscussionTableViewCell *cell = (DiscussionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {

                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DiscussionTableViewCell" owner:self options:nil];
                cell = [topLevelObjects objectAtIndex:0];
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
                // [cell.btnConnect addTarget:self action:@selector(btnUserConnectClick:) forControlEvents:UIControlEventTouchUpInside];
                // [cell.btnNotnow addTarget:self action:@selector(btnNotNowClick:) forControlEvents:UIControlEventTouchUpInside];
                 self.cellHeight  += cell.frame.size.height;
                NSLog(@"Cell height is %f",self.cellHeight);
Hardik1344
  • 2,917
  • 2
  • 15
  • 31

5 Answers5

4

Follow the below steps :

  1. Change label numberOfLines property to zero

enter image description here

  1. add constraints to label

enter image description here

  1. Change height constraint relationship

enter image description here

  1. Implement these two tableView delegates methods

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

Now run your project and check.

Expected output:

enter image description here

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

Your problem is your cell height and tableview row height are different you can solve this in 2 ways

  • Implementing AutoLayout to your custom cell, by this way your cell will grow/shrink according to your row height.

  • The second method is you can make your row height as same as cell height by using -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath delegate method

    Just return your cell height in the above delegate method it will solve your problem.

arunjos007
  • 4,105
  • 1
  • 28
  • 43
0

If you have added UILabel inside Storyboard to UITableviewCell, make sure you have did all changes as per below instractions:

In Storyboard do this changes: Make sure you have given top, bottom, leading and trailing constrains to the UILabel to UITableViewCell contentView.

  1. Then set number of lines to UILabel to '0' also Line Break to "Word Wrap".

enter image description here

  1. In Controller for UITableViewDelegate heightForRow method:

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
      {
        tableView.estimatedRowHeight = 50;
        return UITableViewAutomaticDimension
      } 
    

This will help to solve above requirement.

0

The code I have used to calculate cell content height dynamically is:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_arrMainTable.count && (indexPath.row < _arrMainTable.count))
    {
        NSDictionary *dict = [_arrMainTable objectAtIndex:indexPath.row];
        return [self expectedLabelHeight:dict];
    }
    return 0;
}

- (CGFloat)expectedLabelHeight:(NSDictionary *)dict
{
    CGSize boundingSize = CGSizeMake(CGRectGetWidth(_faqTableView.bounds)-20.0, CGFLOAT_MAX);
    UIFont *font = [UIFont fontWithName:@"Roboto-Regular" size:17];
    CGRect qstnRect = [dict[@"question"] boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];

    font = [UIFont fontWithName:@"Roboto-Light" size:14];
    CGRect ansRect = [dict[@"answer"] boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];

    return (qstnRect.size.height+ansRect.size.height+30); // 30 is the height of another label whose height is fixed to 30pts.
}
ron27
  • 181
  • 14
-1

There are many ways to design Layout for Cell in tableview with dynamic height

here I am sharing some links related to dynamic height and cell autolayout will help to get out form your issue.

Raywenderlich : https://www.raywenderlich.com/129059/self-sizing-table-view-cells

AppCoda : http://www.appcoda.com/self-sizing-cells/

Some answer form SO :

Hope this will help you to solve your issue.

Community
  • 1
  • 1
CodeChanger
  • 7,953
  • 5
  • 49
  • 80