Question

Im trying to implement dynamically sized tableView cells. The cells have a UILabel (among other things) that holds a variable amount of text. Before I customized the cell I was just using cell.textLabel.text to set the text and the cell resized fine and all the text was shown. I did not have to mess with the label at all. But now I have a custom UILabel inside my cell and the text is being cut off. Ive looked at a lot of answers on here but nothing seems to be working.

Here is my code:

//This method gets the size of the text that will be inside the cell/label. This works fine
-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

    CGSize maximumSize = CGSizeMake(labelWidth, 10000);

    UIFont *systemFont = [UIFont systemFontOfSize:15.0];
    //provide appropriate font and font size
    CGSize labelHeightSize = [text sizeWithFont:systemFont
                             constrainedToSize:maximumSize
                                 lineBreakMode:NSLineBreakByWordWrapping];
    return labelHeightSize.height;
}



//Dynamically changes the cell height. This works fine
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    NSString *reviewText = [[self.reviews objectAtIndex:indexPath.row] objectForKey:@"reviewText"];

    CGFloat textHeight = [self getLabelHeightForText:reviewText andWidth:self.tableView.frame.size.width];

    return (textHeight + 40);
}

//This is what doesnt seem to be working. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Review label
    NSString *reviewText = [[self.reviews objectAtIndex:indexPath.row] objectForKey:@"reviewText"];

    UILabel *reviewLabel = (UILabel *)[cell viewWithTag:102];

    CGFloat reviewLabelHeight = [self getLabelHeightForText:reviewText andWidth:reviewLabel.frame.size.width];

    reviewLabel.frame = CGRectMake(reviewLabel.frame.origin.x, reviewLabel.frame.origin.y, reviewLabel.frame.size.width, reviewLabelHeight);

    reviewLabel.text = reviewText;

    reviewLabel.numberOfLines = 0;

    reviewLabel.lineBreakMode = NSLineBreakByWordWrapping;

    NSLog(@"Height: %f", reviewLabel.frame.size.height);

     NSLog(@"Width: %f", reviewLabel.frame.size.width);


//   cell.textLabel.text = [[self.reviews objectAtIndex:indexPath.row] objectForKey:@"reviewText"];
//   
//   cell.textLabel.numberOfLines = 0;
//    
//   cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;

    return cell;
}

As you can see, ive logged the height and width of the label. The width stays a constant 280 which is correct and the height changes dynamically depending the text. Everything looks as if it should be working fine. But sadly it doesnt. The cell resizes fine. The label (judging from the NSLogs) seems to resize fine, so why does my text get cut off?

Thanks

Was it helpful?

Solution

I have currently resolved this by adding some autolayout constraints to my prototype cell. Turning off autolayout also worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top