在UIViewController中使单元格可单击

时间:2018-03-28 18:40:15

标签: swift uitableview cell viewcontroller

我正在使用UIViewController和Table View / Table View Cell(扩展到委托/数据源)。每当从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)
} 

2 个答案:

答案 0 :(得分:1)

您还必须在viewDidLoad

中设置代理
self.tableView.delegate = self

答案 1 :(得分:0)

您尚未将委托设置为tableview。

将其设置为: 在tableView.dataSource = self之后

tableView.delegate = self