【问题标题】:How to push to a new viewController from UITableView?如何从 UITableView 推送到新的 viewController?
【发布时间】:2020-07-21 11:11:56
【问题描述】:

有人可以帮我告诉我哪里出错了吗?我正在尝试从表格视图推送到新的视图控制器,但它什么也没做,它在控制台中打印,因此它正在注册但它没有推送。

enter image description here

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)
    }
}

【问题讨论】:

    标签: swift uitableview uiviewcontroller pushviewcontroller


    【解决方案1】:

    我认为您忘记添加 UITableViewDelegate,这就是 didSelectRowAt 无法运行的原因。

    只需更改 extension HostedVC: UITableViewDataSource

    extension HostedVC: UITableViewDataSource, UITableViewDelegate

    顺便说一句,你不需要打电话给tableView.deselectRow(at: indexPath, animated: true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      相关资源
      最近更新 更多