Ios 在UITableViewCell内设置UITextView高度时高度不正确
我试图设置UITextView的高度,使其适合视图中的所有文本,而无需滚动。Autolayout似乎在工作,因为它在每个实例中都会调整大小,但问题是它从未获得正确的高度。底部似乎总是缺一块Ios 在UITableViewCell内设置UITextView高度时高度不正确,ios,objective-c,uitableview,autolayout,uitextview,Ios,Objective C,Uitableview,Autolayout,Uitextview,我试图设置UITextView的高度,使其适合视图中的所有文本,而无需滚动。Autolayout似乎在工作,因为它在每个实例中都会调整大小,但问题是它从未获得正确的高度。底部似乎总是缺一块 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"Vid
(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;
}
尝试调用layoutifneedd方法
您需要设置单元格的高度以及文本视图的高度…-CGFloattableView:UITableView*表视图RowAtIndexPath的高度:NSIndexPath*indexPath{}。方法UITextView在UITableViewCell的底部设置了一个约束,因此它可以动态调整高度。是否禁用了在textview中滚动?还可以尝试调用layoutIfNeededYep尝试了这两种方法,但没有成功,文本视图确实会根据内容调整大小。每次似乎都会从底部缺少一块。@ORStudios已为UITableview启用自动布局。self.tableView.rowHeight=UITableViewAutomaticDimension;self.tableView.estimateDroweight=44.0;//设置为您的平均单元格高度
[cell.videoInfo addConstraint:height];
[cell.videoInfo setNeedsUpdateConstraints];
[self.view layoutIfNeeded]; // call this method