【问题标题】:Make Cell Clickable In UIViewController在 UIViewController 中使单元格可点击
【发布时间】:2018-03-28 18:40:14
【问题描述】:

我正在使用 UIViewController 和表格视图/表格视图单元(扩展到委托/数据源)。每当从 MessageController 中单击一个单元格时,它应该被定向到另一个视图控制器,称为 ChatController。即使我正在使用 didSelectRowAt 并添加带有标识符的 segue,它也不会切换到 ChatController。

import UIKit

class MessagesController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var myIndex = 0;
@IBOutlet weak var tableView: UITableView!

var tableData: [MModel] = []

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self

    MData.getData { (data) in
        self.tableData = data
        self.tableView.reloadData()
    }

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

}

func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableData.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MessageCell") as! MessagesCell
    cell.setup(model: tableData[indexPath.row])
    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 375

}
//clicking on cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "segue", sender: nil)
}


}

有问题的行如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "segue", sender: nil)
} 

【问题讨论】:

    标签: swift uitableview cell viewcontroller


    【解决方案1】:

    您还必须在 viewDidLoad 中设置委托

    self.tableView.delegate = self
    

    【讨论】:

      【解决方案2】:

      您尚未将委托设置为 tableview。

      设置为: 之后tableView.dataSource = self

      tableView.delegate = self

      【讨论】:

        【解决方案3】:

        我的问题与我的 viewDidLoad() 中有 GestureRecognizer 有关。经过这么多小时的故障排除后,我意识到破坏代码的是 GestureRecognizer 的“隐藏键盘”代码。

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        • 2011-07-30
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        相关资源
        最近更新 更多