我如何使用 uitableview 单元格来执行 segue

How can i use uitableview cell for performing segue

我 2 周前开始学习 Swift。通过一些课程和 YouTube 视频,我学习了 uitable 视图的基础知识,但我不知道如何使用特定的 table 视图单元格,如按钮。

我想做什么?

我正在尝试为我的项目使用侧边栏菜单(https://github.com/yannickl/FlowingMenu),一切都很酷并且工作正常,而不是使用单元格。

在每个基本教程中,人们都展示了如何将 segue 与 uitableview 一起使用,但他们只使用 2 页并且独立于单元格值,无论单元格中的什么文本调用 segue第二个控制器。

我想在单元格中使用边栏菜单,如果我的单元格值为 "main menu",我想执行 "toMainMenu" segue。

我只知道将单元格与数组一起使用,不知道是否有其他方法可以使用单元格,我的主要目标是特定于单元格的转场。

我看了很多教程,但找不到对我有用的东西。

import UIKit

导入流动菜单

class 侧边栏菜单:UIViewController、UITableViewDataSource、UITableViewDelegate {

@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var surnameLabel: UILabel!
@IBOutlet weak var topBar: UINavigationBar!
@IBOutlet weak var backButtonItem: UIBarButtonItem!
@IBOutlet weak var userTableView: UITableView!
@IBOutlet weak var profilePic: UIImageView!
let CellName  = "Menu"
let mainColor = UIColor(hex: 0xC4ABAA)
var sections = ["Main Menu", "Settings", "Profile"]
override func viewDidLoad() {
    super.viewDidLoad()
    userTableView.delegate = self
    userTableView.dataSource = self
    nameLabel.text = Helper.name
    surnameLabel.text = Helper.surname

    topBar.tintColor              = .black
    topBar.barTintColor           = mainColor
    topBar.titleTextAttributes    = [
        NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 22)!,
        NSAttributedString.Key.foregroundColor: UIColor.black
    ]
    userTableView.backgroundColor = mainColor
    view.backgroundColor          = mainColor
}



// MARK: - Managing the Status Bar

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

// MARK: - UITableView DataSource Methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = sections[indexPath.row]
    cell.contentView.backgroundColor = mainColor
    return cell
}

// MARK: - UITableView Delegate Methods

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Select row at")
}
@IBAction func profileButton(_ sender: Any) {
    performSegue(withIdentifier: "toProfile", sender: nil)
}

}

我想在 "Settings" 单元格单击时执行 "toSettings" segue。

据我了解,您的菜单是一个 table 视图,因此您需要在 table didSelectAt

中转到要打开的视图

您可以使用 override prepare for segue。因此,当您 link 使用 segue 的视图时,您可以为每个视图设置一个标识符,然后可以从那里调用您需要的任何内容。

实现 UITableViewDelegate 协议方法tableView(_:didSelectRowAt:)如下

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   switch sections[indexPath.row] {
   case "Main Menu":
      performSegue(with: "toMainMenu", sender: nil)
   case "Settings":
      performSegue(with: "toSettings", sender: nil)
   case "Profile":
      performSegue(with: "toProfile", sender: nil)
   }
}

如果你需要配置或传递一些数据给目标控制器,你可以覆盖控制器的prepare(for:sender:)方法来获取引用,例如:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == "toMainMenu" {
      let dvc = segue.destination as! MenuViewController
      // now you have a reference
   }
}