ghostpotato ghostpotato - 6 months ago 7
Swift Question

Swift - UITableView not unhiding

I have a UITableView that I hide and unhide depending on whether or not a variable is empty.

if listOfTasks.isEmpty {
table.hidden = true
} else {
table.hidden = false
}


This is in my
viewDidLoad()
method. Hiding the UITableView works fine, but unhiding it doesn't. Here are my files in case you want to take a look.

Thanks in advance!

Answer

viewDidLoad() is only called once, when the view is loading. You need to unhide table whenever the table changes; thus, change

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){   
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        listOfTasks.removeAtIndex(indexPath.row)
        table.reloadData()
        if listOfTasks.isEmpty {
            table.hidden = true
        }
    }
}

to

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){   
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        listOfTasks.removeAtIndex(indexPath.row)
        table.reloadData()
        if listOfTasks.isEmpty {
            table.hidden = true
        } else {
            table.hidden = false
        }
    }
}