krikor Herlopian krikor Herlopian - 1 year ago 110
iOS Question

tableview cell not clicking

class MenuDrawerTableViewCell: UITableViewCell {
@IBOutlet weak var Label3: UILabel!
@IBOutlet weak var Image30: UIImageView!
override func awakeFromNib() {
// Initialization code
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state

I have that class, now I got my viewcontroller which has a table.

class OpenMenu: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableV: UITableView!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
var selected = 0
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var DestVC = segue.destination as! CheckinController
var indexPath: NSIndexPath = self.tableV.indexPathForSelectedRow! as NSIndexPath
let menuItem = menuList[indexPath.row]
DestVC.varView = menuItem.index
let newSwiftColor = UIColor(red: CGFloat(255), green: CGFloat(0), blue: CGFloat(238), alpha: CGFloat(1))
var menuList = [Menu]()
func loadMenu() {
let photo1 = UIImage(named: "checkin")!
let menu1 = Menu(name: "Check In", photo: photo1, index: 20)!
let photo2 = UIImage(named: "checkin")!
let menu2 = Menu(name: "Add Store", photo: photo2, index: 10)!
menuList += [menu1, menu2]
func numberOfSectionsInTableView(tableView: UITableView) -> Int
return 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuList.count // your number of cell here
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "MenuDrawerTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as! MenuDrawerTableViewCell
let menuItem = menuList[indexPath.row]
cell.Image30.image =
cell.Label3.text =
cell.Label3.numberOfLines = 1;
if(0 == selected)
selected = 1
self.tableV.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)
return cell
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
// cell selected code here
print("cell selected")
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuDrawerTableViewCell", for: indexPath as IndexPath) as UITableViewCell
return cell
@IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
self.tableV.delegate = self
self.tableV.dataSource = self
let size:CGFloat = 35.0 // 35.0 chosen arbitrarily
let preferences = UserDefaults.standard
let name1 = ""+preferences.string(forKey: "name")!
label2.text = name1
label1.text = String(name1[name1.startIndex])
label2.textColor = UIColor.white
label1.textColor = UIColor.white
label1.textAlignment =
label1.font = UIFont.systemFont(ofSize: 17)
label1.bounds = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: size, height: size))
label1.layer.cornerRadius = size / 2
label1.layer.borderWidth = 3.0
label1.layer.backgroundColor = newSwiftColor.cgColor
label1.layer.borderColor =
override func viewDidAppear(_ animated: Bool) {
override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

What I want, is detect the click on a tableview. But I am not able to detect anything .I supposedly should get cell selected printed, but i am getting nothing.Why the click function not getting called?

Answer Source

You can check your code you have to implemented two time methods(cellForRowAtIndexPath and cellForRowAt)

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download