保存按下按钮状态UITableViewCell

时间:2015-02-05 04:02:32

标签: ios objective-c iphone uitableview

我使用动态单元格UITableView,单元格是自定义UITableViewCell子类的对象。 每个单元格在UILabel内包含UIImageViewUIButtonUIImageView。为清楚起见,它是一个播放流音频的音乐播放器。我的问题是保存按下的按钮状态,例如当我的UITableView首次加载所有按钮都在"取消选择"状态和每个按钮都有" play.png"作为图像,如果我按下此按钮,其图像将变为" pause.png"和目标也改变了。它非常明显,但是如果我向上或向下滚动UITableView,我会看到单元格"暂停"按钮,即使我从未按下这些按钮。我知道我必须在NSMutableArray中保存单元格按钮stae并且我已经这样做了,但我想我做错了。请帮我解决这个典型问题。

我的来源:

播放/暂停切换方法:

-(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];

}

CellForRowAtIndexPath方法:

 - (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;


}

它看起来如何:

当没有按下任何一个按钮时

When no one button is pressed

按播放按钮即可

Press play button and it's ok

向下滚动并在"播放"之后在第四个单元格上看到按下按钮(但实际上没有按下)

Scroll down and see that on the fourth cell after "play" button is pressed too (but actually it's not)

向上滚动,看到所选按钮状态在靠近"播放"真的很紧迫 Scroll up and see that selected button state moved on cell near "play" was really pressed

1 个答案:

答案 0 :(得分:4)

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

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];

}