在UITableViewCell中设置UITextView高度时,高度不正确

时间:2017-11-06 11:13:43

标签: ios objective-c uitableview autolayout uitextview

我正在尝试设置UITextView的高度,以便它适合视图中的所有文本而无需滚动。 Autolayout似乎有效,因为它调整了每个实例的大小,然而问题是它从来没有达到正确的高度。底部似乎总有一块缺失。

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"VideoContentCell";

    VideoContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[VideoContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    /// ----------
    /// Setup the video title
    /// ----------

    [cell.videoTitle setText:[self.videoObject.videoTitle capitalizedString]];


    /// ----------
    /// Setup the channel image
    /// ----------

    [cell.imageChannel.layer setCornerRadius:15.f];

    if(self.videoObject.videoPublisherThumbnail != (id)[NSNull null]){
        [cell.imageChannel sd_setImageWithURL:[NSURL URLWithString:self.videoObject.videoPublisherThumbnail]
                                 placeholderImage:nil];
    }


    /// ----------
    /// Setup the channel title
    /// ----------

    [cell.channelInfo setText:[self.videoObject.videoPublisher capitalizedString]];


    /// ----------
    /// Setup the video description
    /// ----------

    [cell.videoInfo   setText:self.videoObject.videoDescription];

    CGSize sizeThatFitsTextView = [cell.videoInfo sizeThatFits:CGSizeMake(cell.videoInfo.frame.size.width, MAXFLOAT)];


    //Height to be fixed for SubView same as AdHeight
    NSLayoutConstraint *height = [NSLayoutConstraint
                                  constraintWithItem:cell.videoInfo
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute
                                  multiplier:0
                                  constant:sizeThatFitsTextView.height];

    [cell.videoInfo addConstraint:height];

    [cell.videoInfo setNeedsUpdateConstraints];




    return cell;

}

1 个答案:

答案 0 :(得分:0)

尝试调用layoutIfNeeded方法

[cell.videoInfo addConstraint:height];

 [cell.videoInfo setNeedsUpdateConstraints];

[self.view layoutIfNeeded]; // call this method