简体   繁体   English

将数据从 tableviewcell 传递到另一个 viewcontroller

[英]Passing data from tableviewcell to another viewcontroller

I am trying to pass cell's tag data to another viewcontroller's variable我正在尝试将单元格的标签数据传递给另一个视图控制器的变量

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


        let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
       print(currentCell.tag)
        valueToPass = currentCell.tag


    }


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


        let Dest  = segue.destinationViewController as! EventDetail

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

        Dest.eventid = String(valueToPass)
        }

    }

When i try as it is, valueToPass is always passed the previous data.当我按valueToPass尝试时, valueToPass总是传递以前的数据。

Is there a way to get selected cell data from prepareForSegue ?有没有办法从prepareForSegue获取选定的单元格数据?

What is the best way to do that?最好的方法是什么?

You are overriding the indexPath parameter that is being passed to you.您正在覆盖传递给您的 indexPath 参数。

Remove the first let statement.删除第一个 let 语句。 Use the indexPath that is being passed to you.使用传递给您的 indexPath。

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)

{ let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCell { let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCell

//cell.value, cell.text, cell.randomValue or whatever you need } //cell.value、cell.text、cell.randomValue 或任何你需要的东西 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM