在segue IndexPath期间在tableview中传递变量

时间:2016-09-09 04:46:20

标签: ios swift uitableview uistoryboardsegue

我正在尝试从我的tableview中获取数据到视图控制器。到目前为止,我能够移动单元格中显示的数据,但出于某种原因,我无法使用该变量。

以下是已经有效的内容

//Prepare for Segue
destination.titleSegue = (cell?.textLabel?.text!)!
destination.priceSegue = (cell?.detailTextLabel?.text!)!
destination.imageSegue = (cell?.imageView?.image!)!

所有数据都会传输到第二个视图。

现在,我将向您展示我的cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: .Subtitle , reuseIdentifier: cellId)

    cell.backgroundColor = UIColor.clearColor() //Makes colour of cells clear so we can see the background
    cell.textLabel?.text = userList[indexPath.row].title //Shoes Name
    cell.textLabel?.textColor = UIColor.whiteColor() //Sets text colour to white
    cell.detailTextLabel?.text = "$\(userList[indexPath.row].price!)" //Displays price of shoes
    cell.detailTextLabel?.textColor = UIColor.whiteColor() //Sets text colour to white

 //LOOK HERE STACK OVERFLOW*******
 let descriptionText = userList[indexPath.row].description




    let URLString = self.userList[indexPath.row].listingImageURL //Finds URL of profile image

    let URL = NSURL(string:URLString!)! //Converts URL Stirng to NSURL

    cell.imageView?.frame = CGRectMake(10,10,100,100) //Sets frame size for images on tableview TODO

    cell.imageView?.contentMode = .ScaleAspectFit //Sets image to aspect fill for image TODO

    cell.imageView?.hnk_setImageFromURL(URL) //Displays image on table view from Haneke Framework

    return cell
}

如果你回到我的prepare for segue,我需要打电话来解析" descriptionText"来自CellForRowAtIndexPath的变量以使数据移动到第二个视图控制器?

1 个答案:

答案 0 :(得分:2)

正如@vadian所说,在你的prepareForSegue方法中,你可以像这样访问tableView选中的行indexPath

let indexPath = tableView.indexPathForSelectedRow

let descriptionText = self.userList[indexPath.row].description