删除子视图

时间:2018-03-27 01:09:31

标签: uitableview tableview swift4 xcode9 addsubview

我实现了一个子视图,当子表中没有项目时,该子视图显示在UITableViewController的表视图上。添加项目后,子视图将从超级视图中删除以显示tableview,但单元格已变为不可选择。此外,点击“编辑”按钮作为导航项后出现的左侧删除圆圈按钮⛔️对点击无响应。

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

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

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

1 个答案:

答案 0 :(得分:0)

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

noDataScreen.isUserInteractionEnabled = false

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