accessing cell's properties in delegate method

I have a play button, which when pressed, plays a sound, and then changes the image to a pause button. The button is a property of my cell which is in my table view. I'm able to access the cell when the user hits the button because I can do this:

UIButton *b = (UIButton *)sender;
myTableViewCell* cell =  (myTableViewCell*)[[b superview] superview];

And then I can say

[cell.button setImage:myImage];

But I want to change the image of the button back to a play button once the sound is over. I know about the delegate method

-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag 
{
    NSLog(@"audioPlayerDone");
}

And I do get the NSLog when it is completed, but how could I access the cell and change the image? I don't have the luxury of calling superview twice as I did before as there is no sender in this delegate method. Any help??

Answers


The table view represents some content (audio), therefore it makes sense you have a model (object) behind each table view cell.

For each cell you could create an object with 2 or more properties: - audiofile: path to a file - is playing: boolean value

The moment an audio file starts playing (button is pressed) you set the boolean value "is playing" to true and you update the table view so a different image is displayed on the button. To figure out which object you need to change state for, use the method -indexPathForCell:. The code will look somewhat like the following for your button press method:

UIButton *b = (UIButton *)sender;
myTableViewCell* cell =  (myTableViewCell*)[[b superview] superview];

NSIndexPath *indexPath = [tableView indexPathForCell:cell];
AudioObject *audio = [dataArray objectAtIndex:indexPath.row];
audio.isPlaying = true;

[tableView reloadData];

The moment the audio file stops playing you can check the array with models for any file that is playing and set it to false. Afterwards you update the table view again so the button image changes back to the default one.

An implementation like the one I described above will work fine if only 1 audio file is playing at any time.


Need Your Help

How to output periodogram result to a list in Mathematica?

plot wolfram-mathematica fft

I have a time domain data. After I did discrete fourier transform with Periodogram, how can I output the plot into a list for further manipulation?

C# macro-style generics

c# templates generics macros

I am looking for a way to have generics which only exist at compile time for the purpose of code reuse: not having to copy/paste classes and methods. Just text replacement basically, like macros, w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.