Wayne Filkins
Wayne Filkins

Reputation: 337

How to transfer a variable from table cell class to another view through button

I am using Parse and Swift. I have a news feed with posts and each post has an ID. I have a variable that shows the current post ID. I have Navigation views set up in storyboard and when you push a comment button on a post it goes to the comments view.

I have functions set up to get the comments and the only thing I don't have access to is the post ID. All of the questions I find on here say to do an override func to pass the variable to the new view controller, but it doesn't work within the table cell and that is the only place I have access to the post ID. It works to transfer a variable from one UIViewController to another but I need to pass it from the Table View Cell class.

From what I have read you can only pass a "tag" through a button which I think has to be Int, and my ID is a string and always has letters and numbers. I hope this was specific enough, please let me know if I have to include any other information. I have spent several days trying to get this working.

Upvotes: 0

Views: 106

Answers (1)

PaulWoodIII
PaulWoodIII

Reputation: 2656

Are you using a UITableViewController? Its easy to link a cell to the item it is representing with a reference to the UITableViewDatasource's.

func indexPathForCell(cell: UITableViewCell) -> NSIndexPath?

If it is a button in a cell find out what the button's superview is, ,Usually the cell's but you can keep going up until you find a UITableViewCell subclass, and then use the cell to find the indexPath.

A helper function on the Datasource to get the data for cell's indexPath may also be required

Upvotes: 1

Related Questions