相对于UITableview更改UIView高度

时间:2016-06-20 07:23:16

标签: ios swift uitableview uiview constraints

我在AlarmView中有一个名为AlarmView的视图和一个名为alarmtableview的tableview,我也有一个文本字段 现在我想要的是当用户在文本字段中输入任何数字时,将重新加载tableview,其行数等于用户输入的数字。 我最初将AlarmView高度设置为0,因此当文本字段为空时,AlarmView对用户仍然不可见。 重新加载alarmtableview后,我将更新detailview的高度等于tableview的高度。 但是当我运行应用程序时,视图的高度不会更新。即使在重新加载tableview

之后,它仍然对用户不可见

下面是我编写的代码来实现相同的

@IBOutlet weak var AlarmViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var AlarmView: UIView!
@IBOutlet weak var AlarmTableView: UITableView!
@IBOutlet weak var FrequencyField: CustomUITextField!


func textFieldDidEndEditing(textField: UITextField) {

    print("textFieldDidEndEditing called")
    if textField === FrequencyField{
        self.AlarmToggleAction(self.AlarmSwitch)
    }
}
//MARK: ALARM Actions
@IBAction func AlarmToggleAction(sender: UISwitch) {
    if sender.on{
        if let count = Int(self.FrequencyField.text!) {
            print("ON state")
            alarmCount = count
            AlarmTableView.reloadData()

            print("AlarmTableView.frame.height : \(AlarmTableView.frame.height)")
            AlarmViewHeightConstraint.constant = AlarmTableView.frame.height
        }
    }
    else{
        print("OFF state")
        alarmCount = 0
        //AlarmTableView.reloadData()
        //AlarmViewHeightConstraint.constant = 0

    }


}

//MARK: Tableview Actions
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("numberOfRowsInSection called alarmCount : \(alarmCount)")

    return alarmCount
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   print("cellForRowAtIndexPath called")
        let cell = tableView.dequeueReusableCellWithIdentifier("AlarmCell") as! AlarmCell
        cell.TimeLabel.text = "Alarm \(indexPath.row + 1)"
        cell.TimeField.tag = indexPath.row

        cell.TimeSwitch.tag = indexPath.row

        return cell
}

请帮我解决这个问题。 提前致谢

1 个答案:

答案 0 :(得分:0)

尝试使用contentSize代替框架大小,如:

AlarmViewHeightConstraint.constant = AlarmTableView.contentSize.height
相关问题