A.Kumawat A.Kumawat - 1 year ago 98
iOS Question

How to set dynamic label size in UITableViewCell

I have TableViewCell and load data by web service (JSON), Data stored in different-different array and data load in tableview. I want to array data size some time large and some time small. so how will manage label size. I tried may examples but do not make dynamic label. please help, Thank You.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [title count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//NSLog(@"tableview cell");
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"event"];
if (cell==nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [img objectAtIndex:indexPath.row]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.img.image =image;

cell.lbl_1.text=[NSString stringWithFormat:@"%@",[title objectAtIndex:indexPath.row]];

cell.lbl_2.text=[NSString stringWithFormat:@"%@",[des objectAtIndex:indexPath.row]];
cell.lbl_3.text=[NSString stringWithFormat:@"%@",[evnt_ary objectAtIndex:indexPath.row]];

return cell;

Answer Source

Please check UITableViewAutomaticDimension and use it like:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

Make sure your constraints are proper.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download