【发布时间】:2014-12-14 18:43:50
【问题描述】:
对于我的项目,我需要显示一个包含大量信息的医生列表,为此我以编程方式创建了一个自定义单元格。以下是我的单元格视图的结构:
Cell.contentView
| --> UIView
| --> UILabel (Title)
| --> UILabel (Subtitle)
| --> UIView (a simple separator, with a 1px height constraint)
| --> UILabel (Address, multilines)
这是我真正拥有的简化结构,但足以带来警告。
这是警告:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x14f9a790 V:|-(11)-[UILabel:0x16058ea0'DR.NAME'] (Names: '|':DoctorHealthInformationView:0x16058f70 )>",
"<NSLayoutConstraint:0x14f86300 V:[UILabel:0x16058ea0'DR.NAME']-(1.5)-[UILabel:0x160670e0'Chirurgien Dentiste']>",
"<NSLayoutConstraint:0x14f86390 V:[UILabel:0x160670e0'Specialty']-(9)-[UIView:0x14e0fde0]>",
"<NSLayoutConstraint:0x14f9ab20 V:[UIView:0x14e0fde0]-(8)-[KDCellLabel:0x14f90d10'Address\naddres...']>",
"<NSLayoutConstraint:0x14f9ab50 KDCellLabel:0x14f90d10'Address\naddres...'.bottom == DoctorHealthInformationView:0x16058f70.bottom - 10>",
"<NSLayoutConstraint:0x14f97a80 V:|-(0)-[DoctorHealthInformationView:0x16058f70] (Names: '|':CustomDoctorCell:0x1605a890'OnlineCustomDoctorCell' )>",
"<NSLayoutConstraint:0x14f88840 V:[DoctorHealthInformationView:0x16058f70]-(0)-[UIView:0x14f930d0]>",
"<NSLayoutConstraint:0x14f889b0 V:[UIView:0x14f930d0(1)]>",
"<NSLayoutConstraint:0x14f9ae10 UIView:0x14f9d330.bottom == CustomDoctorCell:0x1605a890'OnlineCustomDoctorCell'.bottom>",
"<NSLayoutConstraint:0x14f9b120 V:[UIView:0x14f9d330(4)]>",
"<NSLayoutConstraint:0x14f9af40 V:[UIView:0x14f930d0]-(0)-[UIView:0x14f9d330]>",
"<NSLayoutConstraint:0x14f93020 'UIView-Encapsulated-Layout-Height' V:[CustomDoctorCell:0x1605a890'OnlineCustomDoctorCell'(44)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x14f86390 V:[UILabel:0x160670e0'Specialty']-(9)-[UIView:0x14e0fde0]>
我的限制:
我唯一的高度限制是在我的分隔视图上,标签被固定在所有侧面的超级视图上,以允许我的单元格的高度通过自动布局计算。如果我删除分隔视图并将地址标签的顶部直接固定到字幕标签的底部,警告就会消失。如果我在一个标签上添加高度限制,警告就会回来。
一些经典但可能很有趣的方法:
获取指定行的高度:
表视图控制器:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Doctor *doctor = (Doctor *)[_doctorsToShow objectAtIndex:indexPath.row];
static CustomDoctorCell *sizingCellClassic = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCellClassic = [[CustomDoctorCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:classicDoctorCellIdentifier
forDoctor:doctor
showSpecialty:YES
withAppointmentStyle:YES];
});
[sizingCellClassic updateContentForDoctor:doctor];
return [CustomDoctorCell getCellHeightForCell:sizingCellClassic];
}
CustomDoctorCell:
+ (float) getCellHeightForCell:(CustomDoctorCell *)doctorCell
{
[doctorCell setNeedsLayout];
[doctorCell layoutIfNeeded];
CGSize size = [doctorCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
我不明白我做错了什么,以及我必须做些什么来删除这个警告,我尝试下载this example form raywenderlich.com 并简单地在标签上添加高度限制,我有同样的警告。
还有一点,我必须完全兼容ios 7,所以我没有使用sdk 8引入的新功能。
【问题讨论】:
标签: ios objective-c uitableview ios8