设置单元格高度给我错误

我有UITableViewCellUIView 。 我试图用animation来改变view的高度。 这是我做的:

 self.myViewConstraint.constant = 100; // Coming from 0 [UIView animateWithDuration:0.3f animations:^{ [self.view layoutIfNeeded]; }]; 

唯一的是,它在一个tableView 。 所以这里是我为heightForRow...做的:

 return self.myViewConstraint.constant == 0 ? 74 : 174; 

现在我需要更新tableView ,所以我插入animateWithDuration

 [self.tableView beginUpdates]; [self.tableView endUpdates]; 

因为在cell内部还有其他东西,所以当cells高度animation时, constraints会变得混乱。 我怎样才能更新cells高度,而没有得到的约束搞砸了? (我希望cells高度animation。)

这是我得到的警告。 (一切工作正常,但我只是想删除警告。)

编辑我改变了一下,现在我得到这个错误:

 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:0x7f8d62d15670 V:[tagResultTableView:0x7f8d62d901a0(107)]>", "<NSLayoutConstraint:0x7f8d62cea010 tagResultTableView:0x7f8d62d901a0.top == UITableViewCellContentView:0x7f8d62d8fd50.topMargin - 8>", "<NSLayoutConstraint:0x7f8d62cf0760 V:[tagResultTableView:0x7f8d62d901a0]-(8)-[UILabel:0x7f8d62d2e2e0'Limit: Up to 5 tags']>", "<NSLayoutConstraint:0x7f8d62cf0800 UILabel:0x7f8d62d2e2e0'Limit: Up to 5 tags'.bottom == UITableViewCellContentView:0x7f8d62d8fd50.bottomMargin>", "<NSLayoutConstraint:0x7f8d62f726d0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f8d62d8fd50(32)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f8d62d15670 V:[tagResultTableView:0x7f8d62d901a0(107)]> 

这是一个例子。

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return self.myViewConstraint.constant == 0 ? 74 : 174; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseID]; if (!cell) { cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ReuseID]; } if (self.myViewConstraint.constant != 0) { [cell doAnimation]; } return cell; } 

你可以做的是

  1. 创build一个自定义的UITableViewCell并为“CustomView”的高度contentView创build一个出口@property(弱,非primefaces)IBOutlet NSLayoutConstraint * heightConstraint; 并设置所有的约束。

  2. viewDidLoad设置如下

    tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = N; // N is some estimated height of your cells

  3. 通过更改updateContraints方法中的IBOutlet常量来animation高度heightConstraint.constant = newHeight来做到这一点,无论何时您需要更新UIView的高度,您都可以

    [UIView animateWithDuration:animationDuration animations: ^{ [self.contentView layoutIfNeeded] // This will call to updateContraints you should calculate the new height inside that method }];

  4. 最后打电话给

    [tableView beginUpdates]; [tableView endUpdates];