Reputation: 3
I have a tableview with one button (for play/pause) with two textfields. while click on play for cell1..the button text is play and the song is playing.
but I want to change the button text from play to pause while click on cell2 or any other cell.
Once the button labeled "Play" is pressed it changes to "Pause" and audio is played, and if another button is pressed in other cell the previous cell button SHOULD changes its label to "Play".
It means I want to change the button text/images depending on the play/pause as we see on our music player..Please help
I have written this. Please let me what to do next:
Blockquote
========Updated answer:
- (IBAction)playCallBack:(id)sender {
UIButton *clickedBtn = (UIButton*)sender;
NSIndexPath *indexPath =[self.tableView indexPathForCell:
(UITableViewCell *)[[sender superview] superview]];
NSLog(@"indexpath %ld",(long)indexPath.row);
if (_playBtn != nil && _playBtn != (UIButton*)sender) {
[_playBtn setTitle:@"Play" forState:UIControlStateNormal];
}
if (!player)
{
NSString *path=[[NSBundle mainBundle]
pathForResource:@"iphone_6_original" ofType:@"mp3"];
NSURL *url=[NSURL URLWithString:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setDelegate:self];
[player prepareToPlay];
}
if(_playBtn == clickedBtn)
{
if ([player isPlaying])
{
[clickedBtn setTitle:@"Play" forState:UIControlStateNormal];
[player pause];
}
else
{
[clickedBtn setTitle:@"Pause" forState:UIControlStateNormal];
[player play];
}
}
else
{
if ([player isPlaying]) {
player.currentTime = 0;
[player play];
[clickedBtn setTitle:@"Pause" forState:UIControlStateNormal];
}
else{
[player play];
[clickedBtn setTitle:@"Pause" forState:UIControlStateNormal];
}
}
_playBtn = (UIButton*)sender;
[_tableView reloadData];
}
Upvotes: 0
Views: 92
Reputation: 1805
Create new UIButton
@property (strong, nonatomic) UIButton *currentButton;
Then in action method
- (IBAction)playCallBack:(id)sender {
NSIndexPath *indexPath =[self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSLog(@"indexpath %ld",(long)indexPath.row);
UIButton *clickedBtn = (UIButton*)sender;
if (self.currentButton != nil && self.currentButton != clickedBtn) {
[self.currentButton setTitle:@"Play" forState:UIControlStateNormal];
}
if (!player)
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"iphone_6_original" ofType:@"mp3"];
NSURL *url=[NSURL URLWithString:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setDelegate:self];
[player prepareToPlay];
}
if(self.currentButton == clickedBtn){
if ([player isPlaying])
{
[sender setTitle:@"Play" forState:UIControlStateNormal];
[player pause];
}
else
{
[player play];
[sender setTitle:@"Pause" forState:UIControlStateNormal];
}
}else{
if ([player isPlaying]) {
[player pause];
}
player.currentTime = 0;
[player play];
}
self.currentButton = (UIButton*)sender;
}
Like this you can maintain previous click button and change it according to your requirment when click on another button
Upvotes: 0
Reputation: 167
- (IBAction)playCallBack:(id)sender {
NSIndexPath *indexPath =[self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSLog(@"indexpath %ld",(long)indexPath.row);
if (!player)
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"iphone_6_original" ofType:@"mp3"];
NSURL *url=[NSURL URLWithString:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setDelegate:self];
[player prepareToPlay];
}
if (![player isPlaying])
{
[sender setTitle:@"Play" forState:UIControlStateNormal];
[player play];
}
else
{
[player pause];
[sender setTitle:@"Pause" forState:UIControlStateNormal];
}
[tableview reloadData];
}
if you reload when a button is pressed. and get also current index then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableViewIdentifier=@"cell";
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
if (cell==nil)
{
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
}
cell.musicName.text=[musicList objectAtIndex:indexPath.row];
cell.singer.text=[singerList objectAtIndex:indexPath.row];
cell.duration.text=[durationList objectAtIndex:indexPath.row];
if(currentIndex==indexpath.row)
{
//do
}
else
{
//or change anything
}
[cell.playBtn addTarget:self action:@selector(playCallBack:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Upvotes: 1
Reputation:
- (IBAction)buttonClicked:(id)sender {
yourButton.backgroundColor = [UIColor blackColor];
}
Upvotes: 0