iOS - 带有 UITextView 的 TableView 单元格未正确调整大小

标签 ios textview height tableview cell

我已经对一个单元类进行了子类化,并且正在使用 Storyboard 中的原型(prototype)。有问题的单元格有一个嵌入的 TextView ,它将显示不同长度的文本。 我已经在 heightForRowAtIndexPath: 中设置了单元格高度,但是尝试在 cellForRowAtIndexPath: 中调整 TextView 本身的大小无法正常工作。 它不会在初始加载时调整大小,但一旦我将单元格滚动出 View 然后再返回时就会调整大小。但是如果我继续来回滚动,它偶尔会再次缩小。

代码:

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

最佳答案

事实证明,使用自动布局允许我将顶部空间和底部空间设置为 super View (单元格 View ),并且由于单元格高度在 heightForRowAtIndexPath: 中发生了变化: 然后 TextView 会自动调整大小.

关于iOS - 带有 UITextView 的 TableView 单元格未正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14945216/

相关文章:

iphone - 使用 google 访问 token 进行 Xmpp gtalk 身份验证

android - 如何在 Android TextView 中将文本换行到下一行?

android - 如何在 edittext 字段中更改文本后更新 textview

android获取带有角度的文本的高度和宽度

css - 如何在保持动态子 div 100% 高度的同时调整父 div 的大小

html - Div 父级高度的 100%

iphone - 在 Coredata 中预加载数据的最佳实践

iphone - 如何像 ibooks 应用程序一样突出显示 epub 中的文本

ios - Image.grayscale(1.0) 在 SwiftUI 中返回全彩色图像

swift - 中心和粗体 TextView swift