【问题标题】:Pass variable in tableview during segue IndexPath在 segue IndexPath 期间在 tableview 中传递变量
【发布时间】:2016-09-09 17:15:02
【问题描述】:

我正在尝试将数据从我的 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,我需要调用什么来解析来自CellForRowAtIndexPath 的“descriptionText”变量以获取数据以移动到第二个视图控制器?

【问题讨论】:

  • 你在哪里调用你的performSegue代码?
  • 一如既往,不要从视图(单元格)获取数据,从模型(userList)获取数据。根据执行 segue 的方式(直接或通过didSelect...),您需要通过sender 参数中的单元格或通过tableView.indexPathForSelectedRow 获取prepareForSegue 中的索引路径

标签: ios swift uitableview uistoryboardsegue


【解决方案1】:

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

let indexPath = tableView.indexPathForSelectedRow

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2020-09-04
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多