Swift:将UITableViewCell标签传递给新的ViewController

标记

我有一个UITableView,它使用基于JSON调用的数据填充单元格。像这样:

var items = ["Loading..."]
var indexValue = 0

// Here is SwiftyJSON code //

for (index, item) in enumerate(json) {
    var indvItem = json[index]["Brand"]["Name"].stringValue
    self.items.insert(indvItem, atIndex: indexValue)
    indexValue++
}
self.tableView.reloadData()

如何在选定单元格时获取其标签,然后将其传递给另一个ViewController?

我设法得到:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow();
    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;

    println(currentCell.textLabel.text)
}

我只是想不出如何将其作为变量传递给下一个UIViewController。

谢谢

泽尔·B。

在两个视图控制器之间传递数据取决于视图控制器如何彼此链接。如果它们与segue链接,则需要使用performSegueWithIdentifier方法并覆盖prepareForSegue方法

var valueToPass:String!

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow();
    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;

    valueToPass = currentCell.textLabel.text
    performSegueWithIdentifier("yourSegueIdentifer", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "yourSegueIdentifer") {

        // initialize new view controller and cast it as your view controller
        var viewController = segue.destinationViewController as AnotherViewController
        // your new view controller should have property that will store passed value
        viewController.passedValue = valueToPass
    }

}

如果您的视图控制器未与segue链接,则可以直接从tableView函数传递值

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow();
    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
    let storyboard = UIStoryboard(name: "YourStoryBoardFileName", bundle: nil)
    var viewController = storyboard.instantiateViewControllerWithIdentifier("viewControllerIdentifer") as AnotherViewController
    viewController.passedValue = currentCell.textLabel.text
    self.presentViewController(viewContoller, animated: true , completion: nil) 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将CoreData密钥传递给新的ViewController

将数组从 viewController.swift 传递给 tableViewController.swift

如何使用 prepareForSegue (Swift 4, Xcode 9) 将 UIButton 的标签(一个 Int)传递给其他 ViewController

在使用Swift关闭ViewController的过程中,如何将值从ViewController B传递给ViewController A?

(iOS + Firebase) 无法将图像从 UITableViewCell 传递给下一个 ViewController

将 TableViewCell 传递给新的 TableView(在顶部)Swift

Swift场景userActivity NSUserActivity传递给viewController

将事件传递给 UITableView 之外的 UITableViewCell

将数据传递给ViewController

使用UITapGestureRecognizer,UIImageView和UITableViewCell将参数传递给Swift中的选择器

将地理位置从Swift 2 ViewController传递给Javascript方法

将值从 swift viewcontroller 传递给 swiftui 然后再返回

将数据从TableViewCell标签传递到ViewController

Swift / iOS:如何将数据从 AppDelegate 传递到 ViewController 标签?

如何将图像和标签从一个 viewController 传递到 tableViewCell swift?

将值传递给隐藏标签

将变量传递给标签django

无法将标签传递给ARM宏?

将标签传递给Gradle黄瓜任务

将变量传递给标签Jekyll

将变量传递给href标签

将模板标签传递给子模板

尝试将参数传递给新的EventHandler

将 Blob 传递给新的音频

将新值传递给对象

将 ArrayList<Person> 传递给新活动

在Swift中将对象传递给ViewController

初始化时将数据传递给UITableViewCell

将UICollectionView触摸事件传递给其父UITableViewCell