php - 将标签数据从 TableView 传递到另一个 View Controller

标签 php ios mysql swift

我使用此视频作为示例来说明我对下面的代码所做的操作。 Swift To MYSQL using PHP当从 PHP 获取数据到 tableview 时,我使用了视频的后半部分。我能够让 TableView 在 TableView 单元格行中成功显示来自 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.ini 中提取的数据。我的问题是,我可以使用按钮将数据从标签推送(或显示)到另一个 View Controller 吗?有点像购物车,我想在行中使用一个按钮将标签添加到另一个 View Controller ,这样我就可以在那里查看它(作为另一个标签或其他方法)。

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

最佳答案

是的。可能。

您想将数据从 Tableview 传递到 Next View Controller。对吧?

编写 Tableview 委托(delegate)方法

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)

}

希望对您有所帮助。

关于php - 将标签数据从 TableView 传递到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36649650/

相关文章:

php - 这个 jquery 有问题吗?

php - Paypal Masspay 问题

ios - 使用终端构建 iOS 应用程序

ios - 更改 iOS 系统键盘背景图像和按钮图像

mysql - 从 MySQL 数据库获取给定日期一段时间内的信息

MySQL :GROUP BY WITH ROLLUP ERROR

sql - 使用 INNER JOIN 从 SQL 转换 UPDATE 以便在 MySQL 中使用

javascript - 使用 HTML 按钮的 Ajax 日志数据

PHP - 第 60 行出现意外的 Else

ios - 动画运行时在 iOS 应用程序的主线程上暂停执行