ios - 在 Swift 的 TableView 中使用 segue 发送标签文本数据

标签 ios iphone swift uitableview

晚上好。

我在 Swift 中有一个 TableView,它列出了一些项目。我想,当用户点击它时,显示一个新的 ViewController,其中包含有关该项目的详细信息(如名称、照片、描述)。所以,这是我的 tableView 函数:

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cellExample", forIndexPath: indexPath) as! ExampleTableViewCell

    //Remove separators margins
    cell.layoutMargins = UIEdgeInsetsZero

    cell.labelExample.text = listExample[indexPath.row]

    return cell
}

我做了一些研究,发现也许我可以用那个函数来做到这一点:

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

但是,我如何才能将所选项目(在本例中为标签)发送到新的 viewController,例如,我将在其中为标题添加其他标签?

最佳答案

你可以在这个函数中这样做:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let text = listExample[indexPath.row]
    self.performSegueWithIdentifier(yourSegueIdentifier, sender: text)
}

然后你覆盖它:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == yourIdentifierAbove {
        let controller = segue.destinationViewController as! YourControllerType
        let textPass = sender as! String

        controller.variableWillReceive = textPass
    }

}

关于ios - 在 Swift 的 TableView 中使用 segue 发送标签文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567409/

相关文章:

iphone - 如何在 UITableView 透明 header 下屏蔽 UITableViewCells

ios - 为什么在 ARC 中发送消息会导致保留周期警告,但属性集不会?

ios - 方法 'application:openURL:options:'未被调用

ios - 当应用程序被杀死而不在IOS后台运行时如何获取通知有效负载

c++ - 如何将元素从 Swift 中的 [UnsafeMutablePointer<UInt8>] 转换为 C++ 中的 UInt8 *

html - ios - 在保留 HTML 属性的同时编辑 NSMutableAttributedString

objective-c - 如何从另一个类访问 xib 中的 UIPicker?

iphone - 如何将应用程序的铃声添加到 iPhone 的铃声中?

iphone - Objective-C 中的动态对象属性名称

iphone - iOS:如何跟踪用户创建的书签?