iOS - UITextView 不正确地调整大小的 Table View Cell

4
我已经对一个单元格类进行了子类化,并且正在使用从Storyboard中获取的原型。该单元格包含一个嵌入式文本视图,用于显示不同长度的文本。
我已经在heightForRowAtIndexPath:方法中设置了单元格高度,但是在cellForRowAtIndexPath:方法中尝试调整文本视图大小并不能正常工作。
它无法在初始加载时重新调整大小,但是一旦我将单元格滚动出视图,然后再滚回来,它就会重新调整大小。但如果我一直来回滚动,它偶尔会重新缩小。
代码:
-(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;
}

你正在使用自动布局吗? - dthien
1
通过在Storyboard中添加文本视图并设置正确的约束,这将更容易完成。它将随着单元格自动增长,因此您只需要计算单元格高度即可。 - rdelmar
是的,我正在使用自动布局。虽然对它还有些陌生。适当的约束条件是什么?该单元格基本上与Facebook动态消息单元格相同(用户图像/名称在顶部,文本视图在中间,链接横幅视图在底部)。 - JimmyJammed
1个回答

3
事实证明,使用自动布局使我只需要将上边距和下边距设置为父视图(单元格视图),由于单元格高度在heightForRowAtIndexPath:中更改,因此文本视图会自动调整大小。

这帮助我解决了我的确切问题!非常感谢! - planewalker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接