iOS-带有UITextView的表视图单元格的大小调整不正确



我已经对一个单元格类进行了子类化,并且正在使用Storyboard中的原型。有问题的单元格有一个嵌入的文本视图,它将显示不同长度的文本。我已经在heightForRowAtIndexPath:中设置了单元格高度,但尝试在单元格ForRowAtIndex Path:

代码:

-(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;
}

事实证明,使用自动布局,我只需将顶部空间和底部空间设置为超视图(单元格视图),并且由于单元格高度在heightForRowAtIndexPath:中发生了更改,因此文本视图会自动调整大小。

最新更新