以编程方式从一个 ViewController 导航到 didSelectRowAt 上的另一个

Posted

技术标签:

【中文标题】以编程方式从一个 ViewController 导航到 didSelectRowAt 上的另一个【英文标题】:Navigate from a ViewController to an another on didSelectRowAt programmatically 【发布时间】:2018-10-27 10:15:49 【问题描述】:

我有一个 UIViewController,其中包含一个自定义 UITableView。该表也有一个自定义的UITableViewCell

当您选择/单击其中一行时,如何从第一个 ViewController 导航到另一个?

注意

我没用过StoryBoard

更新

这是我的代码。每个类都是外部文件。如果您需要更多代码,请告诉我。

class TestViewController: UIViewController, UITableViewDelegate 

    override func viewDidLoad() 
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(testTableView)
    

    let testTableView: TestTableView = 
        let table = TestTableView()
        table.register(TestTableViewCell.self, forCellReuseIdentifier: TestTableViewCell.identifier)
        return table
    ()


class TestTableView: UITableView,  UITableViewDelegate, UITableViewDataSource 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return 1
    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
       let cell = tableView.dequeueReusableCell(withIdentifier: TestTableViewCell.identifier, for: indexPath)
        return cell
    




class TestTableViewCell: UITableViewCell 
    static let identifier  = "testCell"

【问题讨论】:

Swift programmatically navigate to another view controller/scene的可能重复 到目前为止你有什么尝试? @MoAbdul-Hameed 你如何从控制器中选择行? @mahan 您需要使当前的视图控制器(包含表格视图的那个)符合UITableViewDelegate 并实现其didSelectRowAtIndexPath 方法。 @RakeshaShastri 添加了一些代码。 【参考方案1】:

这是一个完整的答案:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    let newViewController = NewViewController()
    self.navigationController?.pushViewController(newViewController, animated: true)

【讨论】:

你不要调用这个方法,它是一个委托方法。您需要将表视图的委托设置为您的第一个视图控制器。在viewDidLoad 添加:self.tableView.delegate = self【参考方案2】:

UIKit 中,为了以编程方式从TableCell 导航,您可以在没有情节提要的情况下进行操作。 你有两种方式查看下一个viewController

    如果你想介绍......

动画从下到上 (要记住的一件事)您不能在不关闭之前呈现的屏幕的情况下呈现新视图,否则由于呈现的屏幕冲突而导致应用程序崩溃

yourTableView.deselectRow(at: indexPath, animated: true)//used for single Tap
let vC = YourViewController (nibName: "" , bundle : nil)
        vC.modalPresentationStyle = .fullScreen //this line is optional for fullscreen
        self.present(vC, animated: true , completion: nil)
    或者如果你想正常查看你的viewController(2是连击)......

动画从右到左

yourTableView.deselectRow(at: indexPath, animated: true)
let vC = YourViewController()
self.navigationController?.pushViewController(vC, animated: true)

【讨论】:

【参考方案3】:
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
     let vc = yourCustomVCName (nibName: "yourCustomVC nibName" , bundle : nil)
     self.present(vc, animated: true , completion: nil)  

【讨论】:

以上是关于以编程方式从一个 ViewController 导航到 didSelectRowAt 上的另一个的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式从 CollectionView 单元推送 viewController 不起作用 - swift

以编程方式从 collectionView Cell didSelectItemAt 推送到新的 ViewController - swift

iOS Swift 从推送通知以编程方式导航到某些 ViewController

以编程方式从 ARC 中的导航堆栈中删除 viewController

在具有多个故事板的应用程序中以编程方式从 Storyboard 加载 ViewController

以编程方式/自动创建 ViewController