Reputation: 3
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:
What I need help with is:
didSelectRowAtIndexPath
Any help will be greatly helpful.
Upvotes: 0
Views: 3605
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
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
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