Search code examples
iostextviewheighttableviewcell

iOS - Table View Cell with UITextView not resizing properly


I have subclassed a cell class and am using prototypes from Storyboard. The cell in question has an embedded textview that will display varying lengths of text. I already setup the cell height in heightForRowAtIndexPath:, but trying to resize the textview itself in cellForRowAtIndexPath: does not work properly. It wont resize on initial load, but will once I scroll the cell out of view then back in. But if I keep scrolling back and forth, it occasionaly shrinks back down again.

Code:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int defaultSize = 0;
    int height = 0;
    if(indexPath.section%2==0){
        defaultSize = 160;
        NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section];
        NSString *string = [currentPost valueForKey:@"detailsText"];
        CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
        height = stringSize.height + defaultSize;
        NSLog(@"String Size Before: %f",stringSize.height);

    }else{
        defaultSize = 270;
        height = defaultSize;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    if(indexPath.section%2==0){
        CellIdentifier = @"NewsFeedText";
    }else{
        CellIdentifier = @"NewsFeedImage";
    }
    NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    [cell setBackgroundView:[[NewsFeedCellBackground alloc] init]];
    [cell.bottomContainer setClipsToBounds:YES];
    [cell.bottomContainer.layer setMasksToBounds:YES];
    [cell.imagePreview.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.bottomContainer setFrame:CGRectMake(0, cell.contentView.frame.size.height-30, cell.contentView.frame.size.width, 30)];

    //Data Object
    NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section];
    //View
    cell.userImage.image = [UIImage imageNamed:[currentPost valueForKey:@"userImage"]];
    cell.userName.text = [currentPost valueForKey:@"userName"];
    cell.createdDate.text = [currentPost valueForKey:@"createdDate"];
    cell.patientName.text = [currentPost valueForKey:@"patientName"];
    cell.detailsText.text = [currentPost valueForKey:@"detailsText"];
    [cell.imagePreview setImage:[UIImage imageNamed:[currentPost valueForKey:@"imagePreview"]] forState:UIControlStateNormal];
    [cell.viewCase addTarget:self action:@selector(displayCase:) forControlEvents:UIControlEventTouchUpInside];

    //Image
    [cell.imagePreview addTarget:self action:@selector(displayImage:) forControlEvents:UIControlEventTouchUpInside];

    //Data
    cell.viewCase.tag = [[currentPost valueForKey:@"caseID"] intValue];

    //Cell Adjustments
    NSString *string = [currentPost valueForKey:@"detailsText"];
    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"String Size After: %f",stringSize.height);
    [cell.detailsText setFrame:CGRectMake(cell.detailsText.frame.origin.x, cell.detailsText.frame.origin.y, cell.detailsText.frame.size.width, stringSize.height+10)];
    [cell.detailsText setBackgroundColor:[UIColor redColor]];

    return cell;
}

Solution

  • Turns out using auto layout allowed me to just set the Top space and Bottom space to the superview (cell view), and since the cell height is changed in heightForRowAtIndexPath: the textview then gets resized automagically.