I'm trying to get my table view cells to adjust their heights based on the content inside the cells.
It seems to be working since the cells have different heights, but I'm having trouble with displaying the text properly.
The table view and the prototype cell is created with storyboard and I have a custom cell class linked to my prototype cell. In my prototype cell I have three labels; Title label, date label and a label that has the dynamic text data. The dynamic text data labels line numbers are set to 0 and line break is set to "Word wrapping" in the storyboard's attributes inspector.
When I run the app the cell heights are all different so I assume the height for row at index path works:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Text data
NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:nil];
CGFloat width = 280;
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height + 2 * 20; // size.height plus 2 times the desired margin
}
And this is my cell for row at index path:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsCell";
NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// If the cell doesn't exists create it
if (cell == nil) {
cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
// Text data
NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];
cell.titleLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"title"]];
cell.dateLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"date"]];
cell.newsTextLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];
return cell;
}
The cell.newsTextLabel.text that prints out the dynamic text data is displayed in the cell, but only the first line.
I've tried some examples of displaying dynamic cell heights for iOS 6.1 and they work fine but I can't get it to work on iOS 7+
So I hope someone can help with this and help me to figure out what I'm doing wrong.
To make the label expand with the cell, you should give it constraints that tie it to the views above and below -- in you case that looks like a constraint to the bottom of the cell, and one to the label (with "Titel...") above it.