krikor Herlopian
krikor Herlopian

Reputation: 731

tableview cell not clicking

UITableViewCell sub class:

class MenuDrawerTableViewCell: UITableViewCell {  
@IBOutlet weak var Label3: UILabel!
@IBOutlet weak var Image30: UIImageView!

override func awakeFromNib() {
    super.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?) {
    print("prepare")
    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 =  menuItem.photo
    cell.Label3.text = menuItem.name
    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")
     print(indexPath)    
}
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() {
    super.viewDidLoad()
   loadMenu()
    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 = NSTextAlignment.center
    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 = UIColor.black.cgColor        
}
override func viewDidAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
    super.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?

Upvotes: 3

Views: 1989

Answers (4)

Ed Ray
Ed Ray

Reputation: 21

I had this problem and noticed that somehow "isEditing" was set to "true" for the tableView in my viewDidLoad code... removing that line of code fixed it.

Upvotes: 0

Yogendra Girase
Yogendra Girase

Reputation: 631

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 {

}

Upvotes: 2

Gourav Joshi
Gourav Joshi

Reputation: 2419

First make sure that you have connected tableview and connected delegate and datasource also. Please remove these lines of code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuDrawerTableViewCell", for: indexPath as IndexPath) as UITableViewCell        
    return cell
}

As already you implemented this method. Hope this will fix your problem.

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

I think you need to selection of tableview as below :

enter image description here

Upvotes: 4

Related Questions