【问题标题】:Swift, Pass UITableView Section to new view controllerSwift,将 UITableView 部分传递给新的视图控制器
【发布时间】:2016-01-30 08:29:42
【问题描述】:

如何在 UITableView 中传递当前单击单元格的部分?

例如,假设我有:

  • 类别
    • 发帖
    • 发布 2
    • 发布 3

category 是部分标题,而帖子的项目符号是 UITableView 中的单元格。

我想将单元格标签和部分传递给新视图。

我知道如何传递标签我似乎无法获取当前部分

或者,有没有办法为每个单元格分配一个额外的值,比如数据库中的 ID?

Short and Sweet,如果可能的话,请把我带到某个地方,我是编程新手,所以所有帮助都可以。

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    我猜在你的第一个 ViewController 中你有你的数据来填写 UITableView。

    有点像

    var sectionTitles = ["Category1", "Category2"]
    var postData = [["post1", "post2"],["anotherPost1", "anotherPost2"]]
    

    单击行时委托方法didSelectRowAtIndexPath: 叫做。然后您可以通过indexPath.section 访问选定的行。

    示例

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as! SecondViewController
    
        vc.sectionTitle = self.sectionTitles[indexPath.section]
        vc.label = self.postData[indexPath.section][indexPath.row]
        self.navigationController?.pushViewController(vc, animated:true)
    }
    

    然后您需要在第二个 ViewController 中创建变量 labelsectionTitle

    class SecondViewController : UIViewController {
        var label:String?
        var sectionTitle:String?
    }
    

    【讨论】:

    • 当我将 end: MyViewController 更改为实际的视图控制器时,我得到“viewContrller”类型的值没有成员部分
    • 我想要的是能够将部分标题“类别”分配给一个变量,以便与单元格标签一起传递给一个新视图
    • 是的,因为您需要在 Viewcontroller 中创建该变量部分。让我编辑我的问题以提供更多详细信息。
    • 你能解释一下这是什么吗? instanceViewControllerWithIdentifier("identifier") as!我的视图控制器
    • 它从您需要在 Storyboard 中提供的标识符中引用您的 UIViewController。因此,您转到创建 SecondViewController 的 Storyboard 并为 Storyboard ID 提供一些唯一标识符。然后,您可以通过上面示例中的代码实例化您的 SecondViewController。如需进一步参考,您应该尝试在 stackoverflow 或 google 上找到它。这是非常基本的,而不是您最初的问题的目标。例如。 stackoverflow.com/questions/17088057/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    相关资源
    最近更新 更多