首页 > 解决方案 > 对表格视图中选择的项目使用 segue

问题描述

我在 StackOverflow 上查看了其他可能的解决方案,但不太认为它们适用于我的问题,或者我不明白的简单是更大的可能性。这是我想要做的。我有一个表格视图,每个部分都有部分和项目。我想根据用户在 tableview 中所做的选择来选择不同的视图控制器。我已经使用以下代码设置了一个示例,其中以 segues“report1Segue”和“report2Segue”为例。


class ViewController: UIViewController {

    @IBOutlet weak var reportTableView: UITableView!

    let reportGroup = ["Dietary", "Weight", "Sleep", "Meditation", "Fitness"]

    let report = [["Calorie Breakdown", "Net Calories"], ["Last 3 Months"], ["Last 3 Months"],["Last 3 Months"],["Exercises by Type"]]

    func numberOfSections(in tableView: UITableView) -> Int {
        return report.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return reportGroup[section]
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "reportSegue" {
            if let reportVC = segue.destination as? Report1ViewController, let indexPath = reportTableView.indexPathForSelectedRow {

                reportVC.dataToDisplay = report[indexPath.section][indexPath.row]
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return report[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = report[indexPath.section][indexPath.row]

        return cell

    }
}

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "report1Segue", sender: nil)
    }

}

任何指导总是非常感谢。

标签: swiftsegue

解决方案


斯威夫特 5.2

对于 a 中的固定数据,UITableView您可以这样做:

// Hard code your IndexPath references
// This will make the code easier to read in the delegate methods
enum TableIndex {
    static let calorieBreakdown =          IndexPath(row: 0, section: 0)
    static let netCalories =               IndexPath(row: 1, section: 0)
    static let weightLastThreeMonths =     IndexPath(row: 0, section: 1)
    static let sleepLastThreeMonths =      IndexPath(row: 0, section: 2)
    static let meditationLastThreeMonths = IndexPath(row: 0, section: 3)
    static let fitnessExercises =          IndexPath(row: 0, section: 4)
}


// Example - perform whatever Segues you actually need
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath {
    case TableIndex.calorieBreakdown:
        performSegue(withIdentifier: "report1Segue", sender: self)
    case TableIndex.weightLastThreeMonths, TableIndex.sleepLastThreeMonths:
        performSegue(withIdentifier: "report2Segue", sender: self)
    default:
        break
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let indexPath = tableView.indexPathForSelectedRow else { return }
    let data = report[indexPath.section][indexPath.row]

    switch segue.identifier {
    case "report1Segue":
        if let dest = segue.destination as? Report1ViewControler {
            dest.dataToDisplay = data
        }
    case "report2Segue":
        if let dest = segue.destination as? Report2ViewControler {
            dest.dataToDisplay = data
        }
    default:
        break
    }
}

推荐阅读