为什么子表视图控制器对触摸和滚动没有响应?

时间:2016-10-16 07:20:03

标签: ios swift uitableview

我有一个基本的视图控制器,代码如下:

class List2ViewController: UIViewController {

    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        segmentedControl.addTarget(self, action: #selector(showDummyViewController), for: UIControlEvents.valueChanged)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func showDummyViewController(){

        let controller = storyboard?.instantiateViewController(withIdentifier: "tableController")
          if let controller = controller {
             controller.view.frame = view.bounds
             view.addSubview(controller.view)
             addChildViewController(controller)
             controller.didMove(toParentViewController: self)
        }
    }
}

正如您所看到的,我正在添加一个恰好是表视图控制器的子视图控制器。该表视图控制器的代码如下:

class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let nib = UINib(nibName: CustomCellTypeIdentifiers.NoResultCell, bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: CustomCellTypeIdentifiers.NoResultCell)

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.NoResultCell, for: indexPath) as! CellType
        return cell
    }

}

我的问题是,当我的表视图被添加为子控制器时,它不响应任何触摸输入。表视图显示但不滚动或选择任何行。此外,我的单元格不会显示在行中。你能解释一下我在这个实现中缺少什么吗?提前谢谢。

下面是结果的图片:enter image description here

1 个答案:

答案 0 :(得分:0)

我在这里看到一个问题:

let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.NoResultCell, for: indexPath)

应该是这样的:

let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.NoResultCell, for: indexPath) as? CustomTableViewCellName

其中CustomTableViewCellName应替换为CustomCellTypeIdentifiers.NoResultCell的值,即您的自定义单元格名称,即您的nibName也是:

let nib = UINib(nibName: CustomCellTypeIdentifiers.NoResultCell, bundle: nil)