【问题标题】:Save pressed button state UITableViewCell保存按下按钮状态 UITableViewCell
【发布时间】:2015-02-05 04:02:31
【问题描述】:

我有 UITableView 和动态单元格,这些单元格是自定义 UITableViewCell 子类的对象。 每个单元格包含 UILabelUIImageViewUIButton 在此 UIImageView 内。为清楚起见,它是一个播放流音频的音乐播放器。我的问题是保存按下的按钮状态,例如当我的UITableView第一次加载时,所有按钮都处于“取消选择”状态并且每个按钮都有“play.png”作为图像,然后如果我按下这个按钮,它的图像会变为“暂停.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];

}

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;


}

它的样子:

当没有一个按钮被按下时:

按下播放键就OK了

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

向上滚动并查看在“播放”附近的单元格上移动的选定按钮状态确实被按下了

【问题讨论】:

  • 我的眼睛好痛..这么大的问题
  • 最好通过isPlaying 之类的属性在AVMDataStore *oneItem 实例本身中保持按下状态(播放/暂停)。因此,在cellForRowAtIndexPath: 方法中,您只需检查该值(isPlaying)即可设置按钮状态。并将IBAction方法中的对应值设置为isPlaying属性。

标签: ios objective-c iphone uitableview


【解决方案1】:

你很亲密。问题是单元格被重复使用并且按钮的状态可能不会被选中。因为暂停图像仅在选择时显示,您只需要确保选择按钮。您还需要确保在需要时取消选择它。希望能解决问题。

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

}

【讨论】:

  • 我怎么会这么瞎?谢谢!我知道解决方案一定很简单)
猜你喜欢
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多