I’m creating a custom table that has a button which allows a user to preview a song when pressed. Most of my code works but I haven’t figured out how to pass the player a particular song corresponding to the row in which the button was pressed.
For instance: if I have two rows and #1 says Jay Z and #2 says Red Hot Chili Peppers, I want to press the button in #1 to play Jay and to press the button in #2 for the Peppers. Simple. My code is flawed and no matter which row’s button I press I can only get the same song to play.
I know why that’s happening, but I don’t know how to solve it. I’m just wondering if anyone could hit me with a few lines that could point me in the right direction.
I can’t use didSelectRowAtIndexPath because I want something else to happen when the row itself is selected.
Will I need to create a method for this or is there something I’ve overlooked?
Thanks!
You could also set the
tagproperty of each button you create duringtableView: cellForRowAtIndexPath:, then when yourbuttonTappedevent is called, look up thesenderand find itstag. Thetagproperty of UIView was provided for just this sort of problem.If you need more information than that, you could create a UIButton subclass that stores any or all information needed about the associated song. Once again, you set that information during
cellForRowAtIndexPath, to be retrieved when the button is tapped.