Search code examples
uitableviewtableviewswift4xcode9addsubview

Tableview not selectable after removing subview


I implemented a subview that is displayed over the table view of a UITableViewController when there are no items in the table. After adding an item, the subview is removed from the superview to reveal the tableview, but the cells have become un-selectable. Also, the delete circle button ⛔️ to the left that appears after tapping the Edit button as a navigation item is unresponsive to taps.

Interestingly, the swipe left to delete works. And swiping left to reveal the delete box and then pressing the box works. Really peculiar behavior indeed. This is what I have:

override func viewDidLoad() {
    super.viewDidLoad()

    // Check if there are any items in the fetchedResultsController. If empty, present noDataScreen over the tableView
    if fetchedResultsController.fetchedObjects!.count == 0 {
        let noDataScreen = EmptyTableView.init(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
        view.addSubview(noDataScreen)
}

// Item Search View Controller Delegate
func itemSearchViewController(_ controller: ItemSearchViewController, didFinishAdding item: Item) {
    if fetchedResultsController.fetchedObjects!.count != 0 {
        if let viewWithTag = self.view.viewWithTag(1000) {
            viewWithTag.removeFromSuperview()
        }
    }
}

// I was hoping the code below would reset the state of the tableview after the subview, the noDataScreen, is removed to allow for tableview row selection
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.reloadData()
}

And I tried the following codes:

// Revised viewWillAppear
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.becomeFirstResponder()

        // I moved the if and if-let statements here since this will be invoked as the popover Item Search View Controller is dismissed.
        if fetchedResultsController.fetchedObjects!.count != 0 {

            if let viewWithTag = self.view.viewWithTag(1000) {
                viewWithTag.resignFirstResponder()
                viewWithTag.removeFromSuperview()
            }
            self.view.isUserInteractionEnabled = true // enable interaction with the tableview to return everything to normal, hopefully
            self.navigationItem.leftBarButtonItem?.isEnabled = true
        }
    }

// Added    
override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.view.becomeFirstResponder()
}

Still to no avail 😕. Somehow adding the subview over the superview impacted the touch functionality of the tableView, that is part of the UITableViewController. Any ideas would be greatly appreciated.


Solution

  • I fixed it! I just had to add this line of code after initializing the noDataScreen UIView at Line #4:

    noDataScreen.isUserInteractionEnabled = false
    

    After noDataScreen is removed from the superview, the view that was behind the noDataScreen responds to user touches.