【问题标题】:Incorrect Height When Setting UITextView Height Inside UITableViewCell在 UITableViewCell 内设置 UITextView 高度时高度不正确
【发布时间】:2017-11-06 11:13:43
【问题描述】:

我正在尝试设置 UITextView 的高度,使其适合视图中的所有文本而无需滚动。自动布局似乎正在工作,因为它会在每个实例中调整大小,但问题是它永远无法获得正确的高度。底部似乎总是少了一个块。

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

}

【问题讨论】:

  • 您需要设置单元格的高度以及文本视图的高度。 . . - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {} 。 .方法
  • UITextView 在 UITableViewCell 的底部设置了一个约束,因此它可以动态调整高度。
  • 您是否禁用了文本视图中的滚动?也尝试调用 layoutIfNeeded
  • 是的,两者都尝试了,但都没有成功,文本视图会根据内容调整大小。它似乎每次都从底部丢失了一大块。
  • @ORStudios 你是否为 UITableview 启用了自动布局。 self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is

标签: ios objective-c uitableview autolayout uitextview


【解决方案1】:

尝试调用layoutIfNeeded方法

[cell.videoInfo addConstraint:height];

 [cell.videoInfo setNeedsUpdateConstraints];

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

【讨论】:

    猜你喜欢
    • 2015-08-29
    • 2011-06-20
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多