swift - 如何从 UITableView 推送到新的 viewController?
问题描述
有人可以帮我告诉我哪里出错了吗?我正在尝试从表格视图推送到新的视图控制器,但它什么也没做,它在控制台中打印,因此它正在注册但它没有推送。
extension HostedVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hostItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "hostedCell", for: indexPath) as! HostedTableViewCell
let post = hostItems[indexPath.row]
cell.eventNameLable.text = post.eventName
cell.eventNameLable.textColor = UIColor(red: 0.42, green: 0.33, blue: 0.56, alpha: 1.00)
cell.dateLable.text = post.startDate
cell.dateLable.textColor = UIColor.init(red: 61/255, green: 75/255, blue: 99/255, alpha: 1)
cell.backgroundColor = UIColor.white
cell.logo.image = UIImage(named: "networkingIMG")
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor(red: 0.73, green: 0.73, blue: 0.73, alpha: 1.00)
cell.selectedBackgroundView = backgroundView
cell.layer.masksToBounds = true
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let iP = hostItems[indexPath.row]
print(iP.eventName)
UserDefaults.standard.set(iP.id, forKey: "SelectedId")
let vc = HistoryCellPageVC(event: iP)
hostedVC?.navigationController?.pushViewController(vc, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
// handle delete (by removing the data from your array and updating the tableview)
hostItems.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
}
}
func openEvent(_ hostItems: Hosted) {
let vc = HistoryCellPageVC(event: hostItems)
navigationController?.pushViewController(vc, animated: true)
}
}
解决方案
我认为您忘记添加UITableViewDelegate
,这就是 didSelectRowAt 不运行的原因。
简单地改变
extension HostedVC: UITableViewDataSource
至
extension HostedVC: UITableViewDataSource, UITableViewDelegate
顺便说一句,你不需要打电话tableView.deselectRow(at: indexPath, animated: true)
。
推荐阅读
- javascript - 从 nodejs 的 mongodb 查询返回值
- android - 由于 android.support.v4.content.FileProvider.parsePathStrategy Android 崩溃
- java - 安卓工作室的jdk8 windows 10
- iis - PWA 清单加载,但我无法获得安装提示
- alluxio - 如何在 Alluxio 2.0 java api 中设置主地址和 19998 端口?
- html - 根据 no 更改 html 5 表中的列间距和字体大小。列的
- msbuild - Azure DevOps 中的配置转换
- android - Android Studio Profiler 中垃圾收集的持续时间为 0
- python - PyQt4 - 如何从 QListWidgetItem 中读取 QLineEdit 文本
- database - Laravel:使用内部连接从数据库中获取多条记录