Ios 保存按下的按钮状态UITableViewCell

Ios 保存按下的按钮状态UITableViewCell,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我使用动态单元格创建了UITableView,这些单元格是自定义UITableViewCell子类的对象。 每个单元格都包含UILabel、UIImageView和UIButton在此UIImageView中。为清晰起见,它是一款音乐播放器,可播放音频流。我的问题是保存按下的按钮状态,例如,当我的UITableView第一次加载时,所有按钮都处于“取消选择”状态,并且每个按钮都有“play.png”作为图像,然后如果我按下此按钮,其图像将更改为“pause.png”,目标也将更改。这很明显,但如

我使用动态单元格创建了
UITableView
,这些单元格是自定义
UITableViewCell
子类的对象。 每个单元格都包含
UILabel
UIImageView
UIButton
在此
UIImageView
中。为清晰起见,它是一款音乐播放器,可播放音频流。我的问题是保存按下的按钮状态,例如,当我的
UITableView
第一次加载时,所有按钮都处于“取消选择”状态,并且每个按钮都有“play.png”作为图像,然后如果我按下此按钮,其图像将更改为“pause.png”,目标也将更改。这很明显,但如果我向上或向下滚动我的
UITableView
,我将看到带有“暂停”按钮的单元格,即使我从未按过这些按钮。我知道我必须将单元格按钮保存在
NSMutableArray
中,我已经这样做了,但我想我做得不对。请帮我解决这个典型的问题

我的消息来源:

播放/暂停切换方法:

-(void)selectSettings:(AVMPlayButton *)sender{

CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:musicTable];
NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
[sender setSelected:!sender.isSelected];

if (sender.isSelected) {
    NSLog(@"SELECTED!");

    if (![selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  ) // HERE IS ARRAY WHICH CONTAINS SELECTED BUTTONS
    {
        [selectedButonsToPlay addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        [sender addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];
        UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
        [sender setImage:pauseBackground forState:UIControlStateSelected];
    }

}else {
    NSLog(@"DESELECTED!");
    if ( [selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
    {
        [selectedButonsToPlay removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        //    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
        NSLog(@"DEselected in didDEselect");
        }
    }

NSLog(@"you just select button");
}


-(IBAction)pausePlay:(AVMPlayButton*)sender{
AVMPlayButton *settings = (AVMPlayButton *)sender;
CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:musicTable];
NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];
UIImage *playBackground = [UIImage imageNamed:@"play.png"];
[cell.playButton setImage:playBackground forState:UIControlStateNormal];
[self pauseStream];

}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"fileCell";
   AVMMusicCell *cell = [self.musicTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[AVMMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

AVMDataStore *oneItem = [musicArray objectAtIndex:indexPath.row];
oneItem = [musicArray objectAtIndex:indexPath.row];


[cell setMusic:oneItem];
cell.songName.text = oneItem.fileName;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = MINT;
[cell setSelectedBackgroundView:bgColorView];

[cell.playButton addTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];

//save cell state for selected CELLS
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];

UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
UIImage *playBackground = [UIImage imageNamed:@"play.png"];
if ([selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
{
    NSLog(@"cellforrow %lu contains selected buttons!",indexPath.row);
  //  NSLog(@"picture in cell %lu button is %@",indexPath.row,cell.playButton.imageView.image);
   // [cell.playButton removeTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
    [cell.playButton addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];

    [cell.playButton setImage:pauseBackground forState:UIControlStateSelected];
}
else{
    NSLog(@"cell %lu does not contain selected buton",indexPath.row);
    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
        [cell.playButton setImage:playBackground forState:UIControlStateNormal];
}


return cell;


}
CellForRowAtIndexPath方法:

-(void)selectSettings:(AVMPlayButton *)sender{

CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:musicTable];
NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
[sender setSelected:!sender.isSelected];

if (sender.isSelected) {
    NSLog(@"SELECTED!");

    if (![selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  ) // HERE IS ARRAY WHICH CONTAINS SELECTED BUTTONS
    {
        [selectedButonsToPlay addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        [sender addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];
        UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
        [sender setImage:pauseBackground forState:UIControlStateSelected];
    }

}else {
    NSLog(@"DESELECTED!");
    if ( [selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
    {
        [selectedButonsToPlay removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        //    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
        NSLog(@"DEselected in didDEselect");
        }
    }

NSLog(@"you just select button");
}


-(IBAction)pausePlay:(AVMPlayButton*)sender{
AVMPlayButton *settings = (AVMPlayButton *)sender;
CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:musicTable];
NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];
UIImage *playBackground = [UIImage imageNamed:@"play.png"];
[cell.playButton setImage:playBackground forState:UIControlStateNormal];
[self pauseStream];

}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"fileCell";
   AVMMusicCell *cell = [self.musicTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[AVMMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

AVMDataStore *oneItem = [musicArray objectAtIndex:indexPath.row];
oneItem = [musicArray objectAtIndex:indexPath.row];


[cell setMusic:oneItem];
cell.songName.text = oneItem.fileName;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = MINT;
[cell setSelectedBackgroundView:bgColorView];

[cell.playButton addTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];

//save cell state for selected CELLS
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];

UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
UIImage *playBackground = [UIImage imageNamed:@"play.png"];
if ([selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
{
    NSLog(@"cellforrow %lu contains selected buttons!",indexPath.row);
  //  NSLog(@"picture in cell %lu button is %@",indexPath.row,cell.playButton.imageView.image);
   // [cell.playButton removeTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
    [cell.playButton addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];

    [cell.playButton setImage:pauseBackground forState:UIControlStateSelected];
}
else{
    NSLog(@"cell %lu does not contain selected buton",indexPath.row);
    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
        [cell.playButton setImage:playBackground forState:UIControlStateNormal];
}


return cell;


}
它看起来是什么样子:

当没有按下一个按钮时:

-(void)selectSettings:(AVMPlayButton *)sender{

CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:musicTable];
NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
[sender setSelected:!sender.isSelected];

if (sender.isSelected) {
    NSLog(@"SELECTED!");

    if (![selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  ) // HERE IS ARRAY WHICH CONTAINS SELECTED BUTTONS
    {
        [selectedButonsToPlay addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        [sender addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];
        UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
        [sender setImage:pauseBackground forState:UIControlStateSelected];
    }

}else {
    NSLog(@"DESELECTED!");
    if ( [selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
    {
        [selectedButonsToPlay removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        //    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
        NSLog(@"DEselected in didDEselect");
        }
    }

NSLog(@"you just select button");
}


-(IBAction)pausePlay:(AVMPlayButton*)sender{
AVMPlayButton *settings = (AVMPlayButton *)sender;
CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:musicTable];
NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];
UIImage *playBackground = [UIImage imageNamed:@"play.png"];
[cell.playButton setImage:playBackground forState:UIControlStateNormal];
[self pauseStream];

}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"fileCell";
   AVMMusicCell *cell = [self.musicTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[AVMMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

AVMDataStore *oneItem = [musicArray objectAtIndex:indexPath.row];
oneItem = [musicArray objectAtIndex:indexPath.row];


[cell setMusic:oneItem];
cell.songName.text = oneItem.fileName;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = MINT;
[cell setSelectedBackgroundView:bgColorView];

[cell.playButton addTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];

//save cell state for selected CELLS
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];

UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
UIImage *playBackground = [UIImage imageNamed:@"play.png"];
if ([selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
{
    NSLog(@"cellforrow %lu contains selected buttons!",indexPath.row);
  //  NSLog(@"picture in cell %lu button is %@",indexPath.row,cell.playButton.imageView.image);
   // [cell.playButton removeTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
    [cell.playButton addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];

    [cell.playButton setImage:pauseBackground forState:UIControlStateSelected];
}
else{
    NSLog(@"cell %lu does not contain selected buton",indexPath.row);
    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
        [cell.playButton setImage:playBackground forState:UIControlStateNormal];
}


return cell;


}

按“播放”按钮即可

向下滚动并查看“播放”按钮按下后的第四个单元格(但实际上不是)

向上滚动,查看“播放”附近的单元格上移动的选定按钮状态是否真的按下了

你非常接近。问题是单元格被重用,按钮的状态可能不会被选择。由于暂停图像仅在选中时显示,因此您只需确保按钮已选中。您还需要确保在需要时取消选中它。希望这能解决问题

if ([selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
{
    NSLog(@"cellforrow %lu contains selected buttons!",indexPath.row);
    //  NSLog(@"picture in cell %lu button is %@",indexPath.row,cell.playButton.imageView.image);
    // [cell.playButton removeTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
    [cell.playButton addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];

    [cell.playButton setImage:pauseBackground forState:UIControlStateSelected];

    [cell.playButton setSelected:YES];
}
else{
    NSLog(@"cell %lu does not contain selected buton",indexPath.row);
    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
    [cell.playButton setImage:playBackground forState:UIControlStateNormal];

    [cell.playButton setSelected:NO];

}

我的眼睛疼。。这是一个大问题,最好通过
isplay
等属性将按下的状态(播放/暂停)保持在
AVMDataStore*oneItem
实例本身。因此,在
cellforrowatinexpath:
方法中,您只需检查该值(
isplay
)即可设置按钮状态。并将
iAction
方法中的相应值设置为
iPlaying
属性。我怎么会如此盲目?非常感谢。我知道解决办法必须简单)