Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何从UITableView推送到新的viewController?_Swift_Uitableview_Uiviewcontroller_Pushviewcontroller - Fatal编程技术网

Swift 如何从UITableView推送到新的viewController?

Swift 如何从UITableView推送到新的viewController?,swift,uitableview,uiviewcontroller,pushviewcontroller,Swift,Uitableview,Uiviewcontroller,Pushviewcontroller,有人能告诉我哪里出了问题吗?我试图从一个表视图推送到一个新的视图控制器,但它什么也不做,它在控制台中打印,所以它注册了,但没有推 我想您忘了添加UITableViewDelegate,这就是为什么didSelectRowAt不运行的原因 简单地改变 扩展主机dvc:UITableViewDataSource 到 扩展主机dvc:UITableViewDataSource,UITableViewDelegate 顺便说一下,您不需要调用tableView.deselectRow(at:indep

有人能告诉我哪里出了问题吗?我试图从一个表视图推送到一个新的视图控制器,但它什么也不做,它在控制台中打印,所以它注册了,但没有推


我想您忘了添加
UITableViewDelegate
,这就是为什么didSelectRowAt不运行的原因

简单地改变
扩展主机dvc:UITableViewDataSource

扩展主机dvc:UITableViewDataSource,UITableViewDelegate

顺便说一下,您不需要调用
tableView.deselectRow(at:indepath,animated:true)

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