user2539989
user2539989

Reputation: 3

How to change a button image after selecting a row from table view - iOS

I am new to Xcode and iOS development and trying to build an app which plays few mp3 files. The corresponding file is played when a row in a tableview is selected. I have created a play button (custom button) with PlayButton Icon. What I am trying to do here is:

  1. When I select a row, the song plays and then the image should change from Play to Pause
  2. So far I have been able to program the Play button such that if the song is playing the button image toggles to Pause/Play.

What I need help with is:

  1. How do I access the custom button from didSelectRowAtIndexPath
  2. How to change the image to a different image.

Any help will be greatly helpful.

Upvotes: 0

Views: 3605

Answers (3)

Marcus Adams
Marcus Adams

Reputation: 53830

The standard (and simple) to reference any static (not on a cell) view such as a UIButton is to enable the Assistant Editor in XCode and CTRL-drag the item to the interface section of the .h file from Interface Builder.

This creates an IBOutlet property for the item. XCode will prompt you to name the property.

If you name the IBOutlet "playButton", then you reference it like self.playButton from the same view controller.

Upvotes: 0

geo
geo

Reputation: 1791

I would recommand to create a custom cell so that every cell can manage it's items by himself. A simple class that inherits from UITableViewCell and holds the entity for the song, the UIButton and some methods. Maybe also a BOOL value for play/pause state.

// CustomCell.h
@interface CustomCell : UITableViewCell
{
    IBOutlet UIButton *playButton;
    id songEntity;
    BOOL isPlaying;
}

-(void)playSong;
-(void)pauseSong;

@end


// CustomCell.m
#import "CustomCell.h"

@implementation CustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
    {
        isPlaying = NO;
        // init button...
        UIImage *img = [UIImage imageNamed:@"playButton.png"];
        [playButton setBackgroundImage:img forState:UIControlStateNormal];
    }
    return self;
}

-(void)playSong
{
    // ...
    UIImage *img = [UIImage imageNamed:@"pauseButton.png"];
    [playButton setBackgroundImage:img forState:UIControlStateNormal];
}

-(void)pauseSong
{
    // ...
    UIImage *img = [UIImage imageNamed:@"playButton.png"];
    [playButton setBackgroundImage:img forState:UIControlStateNormal];
}

//...
@end

Upvotes: 0

Sarim Sidd
Sarim Sidd

Reputation: 2176

Answer to your Part

1 ) Assign a tag to your custom button, lets say '10'

Now on your didSelectRowAtIndexPath try something like this

UITableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
UIButton *playBtn = (UIButton*)[cell viewWithTag:10];   //This way you can acess your custom button

2) Its simple to assign/change image, here is how

    [playbtn setImage:[UIImage imageNamed:@"name of your image"] forState:UIControlStateNormal];

Upvotes: 3

Related Questions