删除子视图后无法选择表视图

Tableview not selectable after removing subview

我实现了一个子视图,当 table 中没有项目时,它显示在 UITableViewController 的 table 视图上。添加项目后,子视图从父视图中移除以显示 table 视图,但单元格已变为未选择table。另外,作为导航项点击编辑按钮后出现的左侧删除圆圈按钮⛔️对点击没有反应。

有趣的是,向左滑动即可删除。向左滑动以显示删除框,然后按该框即可。确实是非常奇怪的行为。这是我的:

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()
}

我尝试了以下代码:

// 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()
}

仍然无济于事。以某种方式在父视图上添加子视图会影响 tableView 的触摸功能,它是 UITableViewController 的一部分。任何想法将不胜感激。

我修好了!我只需要在第 4 行初始化 noDataScreen UIView 后添加这行代码:

noDataScreen.isUserInteractionEnabled = false

从父视图中删除 noDataScreen 后,noDataScreen 后面的视图会响应用户触摸。