简体   繁体   中英

Tableview passing same data on select

I have a tableview that display a list of Parse users by object Id. I am trying to have it so when tapped it will push you to a new view controller, with that corresponding objectId. I have the objectId's printing fine in the cell, and even passing data to the new view controller, but it only is passing the last objectId pulled from the query. If someone can help me figure out how to send that rows objectId that would be helpful. Here is my code:

@IBOutlet weak var objectIdData: UILabel!

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

    let cell:ChatTableCell = self.tableView.dequeueReusableCellWithIdentifier("ChatCell") as! ChatTableCell
    let user = self.friends[indexPath.row]
    self.objectIdData?.text = String(user.objectId!)
    cell.cellTitle?.text = String(user.objectId).uppercaseString
    return cell

}

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


    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let user = self.friends[indexPath.row]
    let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController
    secondViewController.lblDetail = objectIdData
    self.navigationController!.pushViewController(secondViewController, animated: true)

}

So it passes the objectId to the lblDetail on the next view controller, but always the same objectId, not the one associated with that user/row. Thanks in advance.

In didSelectRowAtIndexPath , you are setting a UI element (objectIdData isa UILabel) on the secondViewController .

I believe you just want to set the text of the lblDetail just like you did in cellForRowAtIndexPath .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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