Arn*_*nka 1 objective-c uitableview ios
我在 tableviewcell 内的 UILabels 中截断文本有问题。到目前为止,我已阅读并测试了以下链接中的解决方案(以及许多其他类似链接):
但如果我使用“标准”顶部、前导、尾部和底部自动布局约束设置单元格,我似乎无法让它在第一次加载时工作。但是,如果我取消选中超级视图项的“相对边距”并使用例如等于:默认值,我可以
如果我旋转设备或再次向下和向上滚动,它适用于这两种情况。
标签与自定义单元上的出口相连,并在原型单元中垂直放置;它们每个都有三个超级视图约束,标签之间有一个“标准”的垂直距离。拥抱优先级在最上面的标签上设置得更高,以消除 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; }
Run Code Online (Sandbox Code Playgroud)
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; }
Run Code Online (Sandbox Code Playgroud)
未选中“相对边距”和“等于:默认”的一个小问题是,即使标签中的文本没有被截断,单元格在这些设置下看起来也不是很好,因为边距在我的看法。是否有其他一些“默认”方法可以让这个从 Storyboard 工作,或者我是否必须在约束中为 Equals 使用“幻数”?
此外,如果我以编程方式执行此操作,我将如何使其工作?
据我了解,标准的视觉格式“H:|-[topLabel]-|” 自 iOS 8 起默认为“Relative to margin”。
提前致谢!
我有同样的问题,标签垂直放置在 UITableViewCell 中,并且在旋转或重新加载时正常工作。但是他们第一次没有正确显示。
您分享的链接和我找到的答案对我来说很神奇:https : //stackoverflow.com/a/26351692
在 viewDidLoad 中,
self.tbl_Name.estimatedRowHeight = 200.0f;
self.tbl_Name.rowHeight = UITableViewAutomaticDimension;
Run Code Online (Sandbox Code Playgroud)最后,
- (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;
}
Run Code Online (Sandbox Code Playgroud)所有这些事情,解决了我的问题。希望对你也有帮助。
NOTE::
And more importantly, this issue does NOT arise with Xcode 7 on iOS 9. Apple might have fixed it.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4385 次 |
| 最近记录: |