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 inte.net 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)
}
}
I think that you're missing tableView delegate, add it in viewDidLoad:
tableView.delegate = self
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.