Rajeev
Rajeev

Reputation: 99

Increase the Cell Height based on the label text(Dynamic) in cell OBjective-C

How to increase cell height based on the label text(Dynamic) in cell.In the below code lblAnswer is getting dynammic data, based on this the cell height should increase. Tried below code, but doesn't work for me. TIA

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

    static NSString *CellIdentifier = @"Cell";

    EmpViewCell *cell = (EmpViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {


        cell = [[EmpViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.lblAnswer.lineBreakMode = NSLineBreakByWordWrapping;
        cell.lblAnswer.numberOfLines = 0;
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EmpViewCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (EmpViewCell *) currentObject;
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
            }
        }
    }
    NSMutableDictionary* emmpdict = [_emparr objectAtIndex:indexPath.row];

    cell.lblEmpName.text = [NSString stringWithFormat:@"%@",[emmpdict objectForKey:@"EmployeeName"]];

    cell.lblQuestion.text=[NSString stringWithFormat:@"%@",[emmpdict objectForKey:@"Question"]];

    cell.lblAnswer.text=[NSString stringWithFormat:@"%@",[emmpdict objectForKey:@"Answer"]];

    return cell;

}

Upvotes: 0

Views: 2159

Answers (3)

Sathish Kumar VG
Sathish Kumar VG

Reputation: 2172

For new iOS 8 there is a new way to make this simple.

Objective c:

 - (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      self.tableView.estimatedRowHeight = 40.0; // for example. Set your average height 
      self.tableView.rowHeight = UITableViewAutomaticDimension;
      [self.tableView reloadData];
    }

swift 3.0 :

override func viewWillAppear(animated: Bool) {
    self.tableView.estimatedRowHeight = 40 // for example. Set your average height 
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.reloadData()

} 

Upvotes: 0

If you have added UILabel inside Storyboard to UITableviewCell, then won't get cell as nil. Then some of your code won't execute i.e., if(cell == nil) because cell is not nil.

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:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            tableView.estimatedRowHeight = 100;
            return UITableViewAutomaticDimension;
        }
    

This will help to solve above requirement.

Upvotes: 0

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

You easily achieve this by using UITableViewAutomaticDimension.

Just use below code:

your_tableView.estimatedRowHeight = 100; //or any temorary value as it is going to be replaced after getting dynamic height

your_tableView.rowHeight = UITableViewAutomaticDimension;

NOTE: To work this properly, make sure to apply your all constraints in tableViewCell properly

Upvotes: 1

Related Questions