我在tableviewcell中的UILabel中遇到截断文本的问题。到目前为止,我已阅读并测试了以下链接中的解决方案(以及其他许多类似的解决方案):
但是如果我设置具有“标准”顶部,前导,尾随和底部自动布局约束的单元格,我似乎无法让它在第一次加载时工作。 但是,如果我取消选中superview项目的“相对于保证金”并使用例如等于:默认
如果我旋转设备或向下再向上滚动,则适用于两种情况。
标签与定制单元上的出口连接,并在原型单元中垂直放置在彼此之上;它们各有三个超视图约束,标签之间有“标准”垂直距离。最顶层标签上的拥抱优先级设置得更高,以消除IB中的警告,即使结果无关紧要。
相关代码:
TableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
self.tableView.estimatedRowHeight = self.tableView.rowHeight;
self.tableView.rowHeight = UITableViewAutomaticDimension; }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCellIB *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];
cell.titleLabel.text = @"Veeeeeeeery looooooooooooooooong Tiiiiitle";
cell.messageLabel.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.";
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell; }
TableViewCellIB.m
- (void)adjustSizeToMatchWidth:(CGFloat)width {
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
rect = self.contentView.bounds;
rect.size.height = 99999.0;
rect.size.width = 99999.0;
self.contentView.bounds = rect; }
“相对于保证金”未选中和“等于:默认”的一个小问题是,即使文本未在标签中截断,单元格对这些设置看起来也不是很好,因为边距很远在我看来很大。是否有其他“默认”方法可以从Storyboard中使用它,或者我是否必须在约束中使用“魔术数字”作为等号?
此外,如果我以编程方式执行此操作,我将如何使其工作?
据我所知,自iOS 8起,标准视觉格式“H:| - [topLabel] - |”默认为“相对于保证金”。
提前致谢!
答案 0 :(得分:4)
我遇到了同样的问题,标签垂直放置在UITableViewCell中,并在旋转或重新加载时正常工作。但他们第一次没有正确显示。
您分享的链接和我找到的答案对我来说确实很奇怪: https://stackoverflow.com/a/26351692
在viewDidLoad中,
self.tbl_Name.estimatedRowHeight = 200.0f;
self.tbl_Name.rowHeight = UITableViewAutomaticDimension;
最后,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
// dequeue cell...
// do autolayout staffs...or if the autolayout rule has been set in xib, do nothing
[cell layoutIfNeeded];
return cell;
}
所有这些事情,解决了我的问题。希望它对你也有帮助。
NOTE::
And more importantly, this issue does NOT arise with Xcode 7 on iOS 9. Apple might have fixed it.
答案 1 :(得分:0)
//set constraint properly. don't set fixed width/height.
//also label's property no. lines=0
//tempUserCustomCell is @property
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat heightTest=0.0;
if (!self.tempUserCustomCell) {
self.tempUserCustomCell=[self.lifeLineTableView dequeueReusableCellWithIdentifier:@"homeCell"];
}
//set values to cal. height
self.tempUserCustomCell.userName.text = [self.lifeLineAllPostArray[indexPath.row] valueForKey:@"username"];
self.tempUserCustomCell.messageLabel.text= [self.lifeLineAllPostArray[indexPath.row] valueForKey:@"desc"];
self.tempUserCustomCell.viewsLabel.text=[NSString stringWithFormat:@"Views %d",[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"views"] intValue]];
self.tempUserCustomCell.likeDisLikeComment.text=[NSString stringWithFormat:@"Likes %d Dislikes %d",[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"likes"] intValue],[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"dislikes"] intValue]];
if ([[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] valueForKey:@"comments"] integerValue]==0) {
[self.tempUserCustomCell.buttonCommentsOutlatet setTitle:@"Comment" forState:UIControlStateNormal];
}
else{
[self.tempUserCustomCell.buttonCommentsOutlatet setTitle:[NSString stringWithFormat:@"Comments %d",[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"comments"] intValue]] forState:UIControlStateNormal];
}
self.tempUserCustomCell.messageLabel.font=[UIFont fontWithName:@"Helvetica" size:18];
//layout cell
[self.tempUserCustomCell layoutIfNeeded];
heightTest=[self.tempUserCustomCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightTest;
}