2013-05-10 41 views
0

我有一個自定義UITableViewCell內的三個視圖:self.username, self.postText, self.containerView其中self.containerView是其他兩個超級視圖。我希望它這樣被奠定了:NSLayoutConstraint - 超級視圖的動態高度

Contrainer = self.containerView 
----------------------------- 
|.....5pt-margin............| 
| [self.username]   | 
| [self.postText]*   | 
|.....5pt-margin............| 
---------------------------- 
* Posttext can have a dynamic height - and the superview's height should adapt dynamically based on this height 

我已經試過以下NSLayoutConstraint在我的細胞的init方法:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     [...] 
     NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(self.username, self.postText, self.containerView); 
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(5)-[self.username]-[self.postText]-(5)-|" options:0 metrics:nil views:viewsDictionary]; 
    } 
    return self; 
} 

目前,它崩潰,沒有任何記錄。正確的NSLayoutConstraint應該如何顯示?

+0

我認爲最好的方法是創建自定義UITableViewCell類,然後以您喜歡的方式創建所有視圖。 – Ulugbek 2013-05-10 13:51:36

+0

我有一個自定義的UITableViewCell類。這是我試圖添加我的NSLayoutConstraint的類。 – Anders 2013-05-10 13:59:58

+1

您的代碼不完整,因此難以幫助您追蹤。你應該在NSDictonaryOfVariableBindings和visual格式字符串中使用ivars。此外,您無法調整單元格內的表格視圖行高度,因爲表格視圖控制其行的框架,所以必須在委託中提供行高度。 – 2013-05-10 14:36:27

回答

2

當你傳遞字符串參數constraintsWithVisualFormat:你需要通過ASCII藝術般的視覺格式字符串,以便在您創建self.usernameself.postText應該是id類型的格式@"V:|-(5)-[self.username]-[self.postText]-(5)-|"

相關問題