iOS:在单击它时展开单元格

iOS:在单击它时展开单元格

问题描述:

如果单击按钮,单元格的高度应该增加并且图像应该可见,我有一个带有标签和按钮的单元格。iOS:在单击它时展开单元格

我设置了自动布局,对于单击按钮时更改图像的高度约束。

我在init上使用的约束被正确显示(例如,如果我将常量设置为0或200)。但是如果我改变按钮动作中的常量,则什么也不会发生。

这里是约束

func toggleImageHeight(){ 

     cellIsExpanded = !cellIsExpanded 

     self.imageHeightConstraint.constant = self.cellIsExpanded ? 200 : 0 
     UIView.animate(withDuration: 2) { 
      self.layoutIfNeeded() 
     } 
    } 

你的代码是不正确的逻辑。请试试这个。

self.imageHeightConstraint.constant = self.cellIsExpanded ? 200 : 0 
UIView.animate(withDuration: 2) { 
    self.layoutIfNeeded() 
} 

这会工作。

+0

它没有。对不起 - 在我尝试不同的选项之前,我的代码和你的代码一样。仅反映了awakeFromNib()中的值(self.tableViewHeightConstraint.constant)。后来改变不了。 – netshark1000

+0

我不明白真正想说什么。点击单元格后,你想改变你的约束吗? –

+0

你在哪里改变“self.cellIsExpanded”布尔变量? –