将标签数据从一个表视图传递到另一个视图控制器


Passing label data from a tableview to another view controller

我将使用此视频作为我对下面代码所做操作的示例。Swift To MYSQL使用PHP我在将数据从PHP获取到表视图时使用了视频的后半部分。我能够让表视图在表视图单元格行中成功地显示php中的项目。

import Foundation
import UIKit
import WebKit
class BreakfastGetController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!

var values:NSArray = []
override func viewDidLoad() {
    super.viewDidLoad()
    get();
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func get(){
    let url = NSURL(string: "http://cgi.soic.indiana.edu/~team20/BreakfastGetInfo.php")
    let data = NSData(contentsOfURL: url!)
    values = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
    tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count;
}
//call the specific row and displying in the cell row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell
    let maindata = values[indexPath.row]
    cell.name.text = maindata["name"] as? String
    return cell;
}
}

每个单元格行都有一个标签,该标签显示我从php中提取的数据。我的问题是,我可以用按钮将标签中的数据推送到(或显示)另一个视图控制器吗?有点像购物车,我想在行中使用一个按钮,将标签添加到另一个视图控制器,这样我就可以在那里查看它(作为另一个标签或其他方法)。

我知道可能有类似的关于将数据传递给另一个视图控制器的帖子,但我认为我可以使用(或至少找到)的帖子都与此类似。

是。可能的

您希望将数据从表视图传递到下一个视图控制器。正确的

编写Tableview委托方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var value = contentArray.objectAtIndex(indexPath.row)
    var secondVC : SecondViewController
        = self.storyboard ?.instantiateViewControllerWithIdentifier(identifier: "SecondViewController")
    secondVC.contentValue = value as? String
    self.navigationController ?.pushViewController(secondVC, animated: YES)
}

希望能有所帮助。