Preeti Rani Preeti Rani - 2 years ago 108
Swift Question

Change UIView height with respect to UITableview

I have a View named AlarmView and a tableview named alarmtableview inside AlarmView , also I have a textfield
Now What I want is when a user enters any number in textfield , tableview will be reloaded with number of rows equal to that number entered by the user.
I have set the AlarmView height as 0 initially , so that when the textfield is empty AlarmView remains invisible from the users.
And after reloading the alarmtableview , I am updating the detailview's height equal to tableview's height.
But when I run the app the view's height is not updated. it remains invisible from the users even after reloading the tableview

Below Is the code I have written to achieve the same

@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{
//MARK: ALARM Actions
@IBAction func AlarmToggleAction(sender: UISwitch) {
if sender.on{
if let count = Int(self.FrequencyField.text!) {
print("ON state")
alarmCount = count

print("AlarmTableView.frame.height : \(AlarmTableView.frame.height)")
AlarmViewHeightConstraint.constant = AlarmTableView.frame.height
print("OFF state")
alarmCount = 0
//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

Please help me out of this problem.
Thanks in advance

Answer Source

Try using contentSize instead of frame size like:

AlarmViewHeightConstraint.constant = AlarmTableView.contentSize.height
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download