Lily S.
Lily S.

Reputation: 13

Make Cell Clickable In UIViewController

I am using a UIViewController and a Table View/Table View Cell (extending to delegate/datasource). Whenever a cell is clicked from MessageController, it is supposed to be directed into another View Controller, called ChatController. Even though I am using didSelectRowAt and adding a segue with identifier, it does not switch to the 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)
}


}

The problematic line is as follows:

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

Upvotes: 1

Views: 1318

Answers (3)

Pablo
Pablo

Reputation: 1

my problem had to do with having a GestureRecognizer in my viewDidLoad(). after so many hours of troubleshooting i realize that what was breaking the code was that 'hide keyboard' code of GestureRecognizer.

Upvotes: 0

vishalwaka
vishalwaka

Reputation: 89

You have not set the delegate to the tableview.

Set it as: aftertableView.dataSource = self

tableView.delegate = self

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

You must set the delegate also in viewDidLoad

self.tableView.delegate = self

Upvotes: 1

Related Questions