iOS UITableView动态更改高度

时间:2014-08-07 12:23:48

标签: ios objective-c uitableview height autolayout

在我的VC中,我只有1个TableView和导航栏。我正在使用AutoLayout。单元格是动态添加的。我想根据细胞数改变表的高度。为此我添加了一个高度约束并在我的VC中添加了引用。每当tableview重新加载时,我都会调用以下代码: -

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *agentTvHeightConstraint;  

-(void) adjustHeightOfTableView {
CGFloat height = self.agentChatListTableView.contentSize.height;   // ITS 65
CGFloat maxHeight = self.agentChatListTableView.superview.frame.size.height - self.agentChatListTableView.frame.origin.y - 15; // 15 Bottom Padding to table


// if the height of the content is greater than the maxHeight of
// total space on the screen, limit the height to the size of the
// superview.

if (height > maxHeight)
    height = maxHeight;

// now set the height constraint accordingly
[UIView animateWithDuration:0.25 animations:^{
    self.agentTvHeightConstraint.constant = height;
    NSLog(@"AGENT HEIGHT CONSTRAINT = %f", self.agentTvHeightConstraint.constant); 
   // LOGS 15

    [self.view setNeedsUpdateConstraints];
}];
}

代码执行但表的高度未更改。为什么高度没变?我错过了什么或出错吗?你能指点我吗。

感谢任何帮助。感谢。

3 个答案:

答案 0 :(得分:0)

也许您应该取消选中故事板中的AutoResize子视图。

答案 1 :(得分:0)

在动画块调用中

[self.tableView layoutIfNeed];

它应该有用

答案 2 :(得分:0)

使用过AutoLayout时,已为TrailingLeadingTopBottom设置了约束。已将Bottom约束从Equal更改为Greater Than or Equal。这就是诀窍。代码不变。

希望这有助于某人。

相关问题