我在AlarmView内有一个名为AlarmView的视图和一个名为alarmtableview的表视图,也有一个文本字段
现在我想要的是当用户在文本字段中输入任何数字时,将重新加载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
}

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

最佳答案

尝试使用contentSize而不是帧大小,例如:

AlarmViewHeightConstraint.constant = AlarmTableView.contentSize.height

10-07 14:17
查看更多