2014-12-14 7 views
2

내 프로젝트의 경우 많은 정보가 담긴 의사 목록을 표시해야합니다.이를 위해 프로그래밍 방식으로 사용자 지정 셀을 만듭니다. 다음은 내 셀의보기가 구성되는 방식입니다.autolayout이있는 사용자 지정 UITableViewCell이 경고를 발생시킵니다.

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]> 

내 제약 : 내가 가진 유일한 높이 제약 조건 내 분리보기에

가, 레이블 모든 측면에서 수퍼에 고정되어 내 셀의 높이가 자동 레이아웃에 의해 계산되도록하십시오. 구분 기호보기를 제거하고 주소 레이블의 맨 위를 자막 레이블의 맨 아래에 직접 고정하면 경고가 사라집니다. 하나의 레이블에 높이 제한을 추가하면 경고가 다시 나타납니다.

일부 고전하지만 어쩌면 흥미로운 방법 :

지정된 행의 높이를 얻기 위해 :

TableViewController :

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

I 돈을 ~ 미만 내가 잘못하고있는 것을 지켜라.이 경고를 제거하기 위해해야 ​​할 일은 this example 형태의 raywenderlich.com을 다운로드하고 레이블에 높이 제한을 추가하기 만하면된다. 나는 같은 경고를 받는다. 내가 셀의 초기 높이가 수직보다 더 가능성이 작다는 SDK (8)

답변

1

에 의해 도입 된 새로운 기능을 사용하지 않는 이유

한가지 더, 내가 IOS 7과 완벽하게 호환해야, 그건 제약 조건. contentView의 프레임이 변경 될 때까지 초기 높이 충돌이 발생합니다.

는 다음과 같은 방법 중 하나를 사용하여이 문제를 해결할 수 있습니다 :

  • 는 처음에 내용에 맞게 스토리 보드에서 셀의 높이를 늘립니다.

  • 변경 큰 크기로 세포의있는 contentView의 경계 :

    self.contentView.bounds = CGRectMake(0, 0, 99999, 99999);

당신은 this auto layout question에 대한 답변에서 자세한 내용을 확인할 수 있습니다.

2

스토리 보드에서 프로토 타입 셀을 사용할 때 비슷한 상황이 발생했습니다. contentView의 UIView-Encapsulated-Layout-Height가 모든 것을 망쳤습니다. 내 솔루션은 다음 두 가지를 수행하는 것이 었습니다.

먼저 수직 제약 조건 중 하나의 우선 순위를 999로 설정했습니다.

두 번째로 IB의 표보기 셀에 대해 "사용자 지정"행 높이를 설정합니다. 내 경우

Custom Row Height

, 그것은 266로 설정,하지만 난 정말만큼 당신이 "사용자 지정"확인란이 선택하고는 "기본"을 말하지 않는다 가지고, 중요한 생각하지 않는다 행 높이 입력.

처음에는 낮은 우선 순위 수직 스페이서 제약이 수동으로 입력 한 사용자 정의 높이가 우선한다는 것을 의미 할 것이라고 걱정했지만 처음에는 그렇지 않았습니다. 테이블 셀은 내가 준 제약 조건을 따릅니다.

반 직관적 인 것처럼 보입니다.하지만 내 직감은 사전 높이 계산과 contentView 내부의 수직 구속 조건 사이에 충돌이 발생하지만 layoutSubviews의 레이어 포인트에서 제약 조건이 우선 적용됩니다. 앱은 사업에 대해 기분 좋게 간다. 그래서 앱이 모든 것을 살펴보고 rowHeight 또는 estimatedRowHeight를 만족시키기위한 제약 조건을 해제하려는 시도를 한 다음 처음부터 다시 돌아서 서 수행해야 할 작업을 수행한다고 생각합니다. 하지만 이것은 완전한 추측입니다. 그럼에도 불구하고, 그것은 내 상황에서 효과가 있었다.

당신은 iOS7을 대상으로하고 있다고 언급 했으므로 iOS7에서만 테스트 했으므로이 기능이 사용자에게 도움이 될지 잘 모르겠지만 상황은 매우 유사하여 한 번해볼 가치가 있습니다. 그것이 iOS8이라는 것을 감안할 때, 나는 새로운 기능을 사용하고 있습니다.

self.tableView.rowHeight = UITableViewAutomaticDimension 
    self.tableView.estimatedRowHeight = (UIApplication.sharedApplication().keyWindow?.bounds.height)! * (460.0/1334.0) 
+0

iOS9에서 하나의 높이 제한 조건 우선 순위를 1000 미만으로 설정하면됩니다. 이유를 이해할 수 없지만 자동 레이아웃 제약 조건을 여러 번 확인했는데 문제가 없어야했습니다. UITableViewCell의 자동 레이아웃은 여러 번 두통이되었습니다 ... –

관련 문제