2015-02-05 152 views
1

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

我来源:

播放/暂停切换方法:

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

+0

我的眼睛看不见..这么大的问题 – Vihar

+0

最好是通过像'isPlaying'属性保持'AVMDataStore * oneItem'实例本身的下压状态(播放/暂停)。因此,在'cellForRowAtIndexPath:'方法中,您只需检查该值('isPlaying')来设置按钮状态。并在'IBAction'方法中将相应的值设置为'isPlaying'属性。 – Akhilrajtr

回答

3

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

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

} 
+0

我怎么会这么盲目?谢谢!我知道解决方案必须简单) – vendettacore