swift - 根据所选的 UITableViewCell 打开多个 swift 文件之一

标签 swift uitableview uiviewcontroller swift2 uistoryboardsegue

我有一个应用程序,由许多不同的数组形式的问题组成。我将这些数组放入不同的 swift 文件中,即 QuestionsFile1.swift、QuestionsFile 2.swift、QuestionFile3.swift、QuestionFile4.swift...等。

我有一个 UITableView,它在表格 View 的每个单元格中显示这些不同的部分,即问题 1、问题 2、问题 3、问题 4...等。

我已将其设置为当选择 UITableViewCell 时它将显示与 swift 文件关联的 viewController。然而,这意味着我在 Storyboard上制作了很多viewController。

我怎样才能只拥有一个 viewController 并根据所选的 UITableViewCell 用来自不同 swift 文件的问题填充它?

我选择使用调用 UIViewControllers

           override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if indexPath.row == 0 || indexPath == 1 {
            self.performSegueWithIdentifier("QuestionsFile1", sender: self)
        }


        if indexPath.row == 1 {
            self.performSegueWithIdentifier("QuestionsFile2", sender: self)
        }

        if indexPath.row == 2 {
            self.performSegueWithIdentifier("QuestionsFile3", sender: self)
        }

        if indexPath.row == 3 {
            self.performSegueWithIdentifier("QuestionsFile4", sender: self)
        }

        if indexPath.row == 4 {
            self.performSegueWithIdentifier("QuestionsFile5", sender: self)
        }

        if indexPath.row == 5 {
            self.performSegueWithIdentifier("QuestionsFile6", sender: self)
        }

        if indexPath.row == 6 {
            self.performSegueWithIdentifier("QuestionsFile7", sender: self)
        }
    } 

我只想调用一个 View Controller ,然后根据所选的 UITableViewCell 用各种 swift 文件中的问题填充它。因为我有很多 viewController,而我认为我可以只拥有一个。

最佳答案

是的,你是对的 - 看起来你在问题文件表中更改的只是内容,因此你应该有一个带有 tableView 的单个 View ,并根据你想要的问题填充它。

您可能应该将问题存储在数据文件中,而不是将它们硬编码为 swift 类,因此请考虑阅读和解析应用程序包中的文本文件,或者众多轻量级数据库之一 - Realm 或(可能有点过头了) ) 核心数据。

关于swift - 根据所选的 UITableViewCell 打开多个 swift 文件之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41749186/

相关文章:

ios - ?? Swift 中的运算符

iphone - 使用 CoreData 在 UITableView 中动画行删除会导致断言失败

ios - 在 View Controller 之间传递数据

ios - 在 Storyboard 中创建可重用的 View

ios - 快速取消 AsyncTask 的正确方法

Swift Mac Quick Look 显示图像

ios - 删除 TableView 中的所有本地通知和单元格

iOS swift : Getting repeated value while updating 2D Array in custom UITableView cell

ios - 通过点击任何 View 上的本地通知来呈现 View Controller

objective-c - 将 viewController 插入另一个 viewController 后会发生什么