在 UIViewController 中使单元格可点击
Make Cell Clickable In UIViewController
我正在使用 UIViewController 和 Table View/Table View Cell(扩展到 delegate/datasource)。每当从 MessageController 单击一个单元格时,它应该被定向到另一个视图控制器,称为 ChatController。即使我正在使用 didSelectRowAt 并添加带有标识符的 segue,它也不会切换到 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)
}
}
有问题的行如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segue", sender: nil)
}
您还必须在 viewDidLoad
中设置委托
self.tableView.delegate = self
您还没有为表视图设置委托。
设置为:
之后tableView.dataSource = self
tableView.delegate = self
我的问题与我的 viewDidLoad() 中的 GestureRecognizer 有关。经过这么多小时的故障排除,我意识到破坏代码的是 GestureRecognizer 的 'hide keyboard' 代码。
我正在使用 UIViewController 和 Table View/Table View Cell(扩展到 delegate/datasource)。每当从 MessageController 单击一个单元格时,它应该被定向到另一个视图控制器,称为 ChatController。即使我正在使用 didSelectRowAt 并添加带有标识符的 segue,它也不会切换到 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)
}
}
有问题的行如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segue", sender: nil)
}
您还必须在 viewDidLoad
self.tableView.delegate = self
您还没有为表视图设置委托。
设置为:
之后tableView.dataSource = self
tableView.delegate = self
我的问题与我的 viewDidLoad() 中的 GestureRecognizer 有关。经过这么多小时的故障排除,我意识到破坏代码的是 GestureRecognizer 的 'hide keyboard' 代码。