【问题标题】:tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) is not calledtableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 没有被调用
【发布时间】:2017-09-08 09:25:10
【问题描述】:

我的结构如下:

UIViewController -> UIVIew -> UITableView

tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

被调用但是

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

没有被调用。

class ViewcontrollerA : UIViewController  , UITableViewDelegate , UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.doctortableView.delegate = self
        self.doctortableView.dataSource = self
        self.doctortableView.allowsSelection = true

    }

    func callTable() {

        doctortableView.frame = CGRect(......)
        view1.addSubview(doctortableView)
        doctortableView.isUserInteractionEnabled = true

        self.view.addSubview(view1)

    }

    // Tableview Functions

    // Number of Sections in Table
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        var count = Int()

        return count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = UITableViewCell()

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        //This function is not called
    }
}

【问题讨论】:

  • 你怎么知道它没有被调用,你的数据源没有任何行?
  • 令人惊讶的是,大量答案出现的速度如此之快,显然所有人都被“未调用某些 tableview 委托方法”所吸引,而不是查看代码,所有这些都只是假设 OP 错过了正确连接委托.
  • @luk2302 我真的不明白,为什么人们在阅读完整问题之前就回复了。
  • 因为第一个发帖人“获胜”,如果他们猜对了,那么最快发布答案的人可能会获得最多的支持/接受。
  • @swiftuser123 你有添加手势吗?

标签: ios swift uitableview


【解决方案1】:

乍一看,问题似乎出在这个数据源方法上。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var count = Int()

    return count
}

Int() 不返回 0 吗?这意味着您的表格视图没有任何单元格。

【讨论】:

  • @Jeremy 你不应该写完整的错误方法,而是写这个方法并纠正。
  • 我有一个字典数据,只是这里没有提到这个对象。我确实在 Tableview 中显示了我的数据。问题是我无法选择行。
  • @swiftuser123 不,如果您的行数为0,则无法显示数据。因此,要么您没有向我们展示您正在使用的真实代码,要么您刚才说的是错误的。在这两种情况下,请不要。这个具体问题已经回答了。如果您有与此代码相关的新问题,请提出新问题。 Jeremy 这里已经回答了当前的问题。
【解决方案2】:

根据 Chat 的讨论

您正在以编程方式创建 TableView 并添加到 self.view

func callTable() {

    doctortableView.frame = CGRect(......)
    view1.addSubview(doctortableView)
    doctortableView.isUserInteractionEnabled = true

    self.view.addSubview(view1)

}

但是您的 tableView 不在顶部,因此您无法滚动它,这就是委托不起作用的原因 缺少以下行

self.view.bringSubview(toFront: doctortableView)

这是完整的代码

func callTable() {

    doctortableView.frame = CGRect(......)
    view1.addSubview(doctortableView)
    doctortableView.isUserInteractionEnabled = true

    self.view.addSubview(view1)
    self.view.bringSubview(toFront: doctortableView)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多