Home > other >  Missing print message when clicking the cell Swift TableView
Missing print message when clicking the cell Swift TableView

Time:01-21

just finish a course and this is my first app that I am building, currently I am trying to create a tableView in which each cell is create by an object from a .xib file and when i try to click the cell and print the message from the cell it does nothing.

Can someone give me a hint what I am doing wrong please because I spent hours searching on the internet but I could not find the solution .

Kind regards to everyone.

code down below :

import UIKit

class HomePageViewController : UIViewController, UITableViewDelegate,  UITableViewDataSource {
    
    
    @IBOutlet weak var menuCell: UIView!
    @IBOutlet weak var tableView: UITableView!

        
    var messages : [Message] = [
        Message(message: "Check Menu", destination: "One"),
        Message(message: "Chech order", destination: "One"),
    ]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.separatorStyle = .none
        navigationItem.hidesBackButton = true
        tableView.dataSource = self
        tableView.register(UINib(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
        tableView.rowHeight = 80.0
        
    }
    
    
    
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages.count
    }
    
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! MenuCell
        cell.labelMenu?.text = messages[indexPath.row].message
        return cell
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print(messages[indexPath.row].message)
    }  
}

CodePudding user response:

I think that you're missing tableView delegate, add it in viewDidLoad:

tableView.delegate = self
  • Related