我有一个 tableview,它有自己的类以便可重用。我的问题是现在我想添加:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
let vc = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "eventView") as! eventView
vc.event = self.eventsToLoad[indexPath.row]
self.present(vc, animated: true, completion:nil)
}
但是由于 tableview 有自己的类,我不能自己没有 present 方法。我也不想使用 segue 或 navigationcontroller 或任何东西,以便能够在任何地方使用这个 tableview。
最佳答案
如果您要让 tableView 成为它自己的委托(delegate),那么您必须添加自己的自定义委托(delegate)来通知演示者(在您的情况下是 View Controller )有关 didSelectRowAt 事件。
喜欢:
protocol CustomTableViewDelegate: class {
func customTableView(_ tableView: CustomTableView, didSelectRowAt indexPath: IndexPath)
}
class CustomTableView: UITableView, UITableViewDelegate {
weak var customDelegate: CustomTableViewDelegate?
//... other properties and UITableViewDelegate implementation
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
customDelegate?.customTableView(self, didSelectRowAt: indexPath)
}
}
class SomeViewController: CustomTableViewDelegate {
func customTableView(_ tableView: CustomTableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
let vc = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "eventView") as! eventView
vc.event = self.eventsToLoad[indexPath.row]
self.present(vc, animated: true, completion:nil)
}
}
关于ios - swift 4 : New Viewcontroller when tapped on a cell with separate class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54910420/