2016-07-05 2 views
1

셀 안에 UIWebView가있는 사용자 지정 UITableViewCell이 있습니다. UIWebView에는 기본 높이 제약 조건 (20 픽셀)이 있습니다.로드 후 UITableViewCell 높이 변경

class TableViewCell: UITableViewCell, UIWebViewDelegate { 

    @IBOutlet weak var webView: UIWebView! 
    @IBOutlet weak var webViewHeight: NSLayoutConstraint! 

    func webViewDidFinishLoad(webView: UIWebView) { 

     webView.frame.size.height = 1 
     var contentSize = webView.scrollView.contentSize 

     webView.frame.size.height = contentSize.height 
     webView.scrollView.frame.size.height = contentSize.height 

     webViewHeight.constant = contentSize.height 

    } 
} 

그런 다음 webView에로드 할 항목을 설정합니다. UIWebViewDelegate를 호출 한 후 webViewDidFinishLoad을 호출하고 높이 제한을 변경합니다. 그리고이 순간 디버그 영역에 제약 조건 오류가 표시됩니다.

2016-07-06 09:11:20.492 TEST CELL[1746:56476] 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. 
(
"<NSLayoutConstraint:0x7fbc087088f0 V:[UIWebView:0x7fbc0871eb70(50)]>", 
"<NSLayoutConstraint:0x7fbc086b46f0 UIWebView:0x7fbc0871eb70.top == UITableViewCellContentView:0x7fbc0871dea0.topMargin>", 
"<NSLayoutConstraint:0x7fbc086b4740 UITableViewCellContentView:0x7fbc0871dea0.bottomMargin == UIWebView:0x7fbc0871eb70.bottom>", 
"<NSLayoutConstraint:0x7fbc08700c10 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbc0871dea0(59)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fbc087088f0 V:[UIWebView:0x7fbc0871eb70(50)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

로드 후 올바르게 새로운 제약 조건을 어떻게 설정할 수 있습니까? TableViewCell에서 다음의 제약 상단과 하단에 webView를 고정하여

if (webview.isLoading) { 
    return 
} else { 
    // do my stuff 
} 
+0

제약 조건과 관련된 오류는 무엇입니까? – SArnab

+0

오류 설명이있는 질문 내용을 편집했습니다. –

+0

이 질문을 도와 주시면 문제를 해결할 수 있습니다. 셀 안에는 어떤 제약 사항이 있습니까? 어디서나 59 픽셀의 제한이 있습니까? 그것을 제거하고 시도하십시오 ... 또한 [cell layoutIfNeeded]를 쓰는 것을 잊지 마십시오; 제약에 새로운 값을 할당 한 후 –

답변

0

webViewDidFinishLoad는이 속성을 확인해야합니다, 프레임이 너무로드가 완료 될 때마다 여러 번 호출 할 수 있습니다 이 셀에 표시되는 UITableView와 클래스는

tableView.rowHeight = UITableViewAutomaticDimension 

을 설정하고 012에 제약 조건으로하고 있습니다로 TableViewCellwebView의 높이를 설정.

이렇게하면 셀에 올바른 높이가 부여되므로 마지막으로 일부 델리게이트 패턴을 사용하여 UITableView을 호출하여 리로드 (이 셀의 indexPath를 다시로드하기 만하면 됨)를 호출 할 수 있습니다.

0

당신이 이것을 달성 할 수

+0

물론'tableView.rowHeight = UITableViewAutomaticDimension'을 설정했습니다. 'func webViewDidFinishLoad'에서 모든 webView 프레임 (편집 된 질문. 위로)과 제약에 대한 높이를 설정했습니다. 만약'tableView.reloadRowsAtIndexPaths ([NSIndexPath (forRow : 0, inSection : 0)], withRowAnimation : UITableViewRowAnimation.None)를 다시로드하려고하면'webView가 다시 초기화됩니다. –

관련 문제