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

时间:2016-03-29 01:40:52

标签: ios swift uitableview

我正在尝试将单元格的标记数据传递给另一个viewcontroller的变量

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)
        }

    }

当我按原样尝试时,valueToPass始终传递先前的数据。

有没有办法从prepareForSegue获取选定的单元格数据?

最好的方法是什么?

2 个答案:

答案 0 :(得分:1)

您正在覆盖传递给您的indexPath参数。

删除第一个let语句。使用传递给您的indexPath。

答案 1 :(得分:0)

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

{     让cell = tableView.cellForRowAtIndexPath(indexPath)为YourCell

// cell.value,cell.text,cell.randomValue或您需要的任何内容 }

相关问题