0

以下のコードを使用して、ボリューム ビューをセルに実装しています。

[[cell detailTextLabel] setText: @""];
  MPVolumeView *systemVolumeSlider = [[MPVolumeView alloc] initWithFrame: CGRectMake(100, 10, 200, 100)];
  [cell addSubview: systemVolumeSlider];
  [self.view addSubview:cell];
  [systemVolumeSlider release];
  //[MPVolumeView release];

しかし、私はそれに問題があります。テーブルビューを上下にスクロールするたびに、MPVolumeView が他のセルにも追加されます。どうすればこれを修正できますか?


4

1 に答える 1

0

コメントで述べたように、ボリューム コントロールのあるセルは非ボリューム セルに再利用される可能性があるため、既に存在する場合は削除する必要があります。これを行う方法の例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    //remove the volume control (which we tagged as 10) if it already exists...
    UIView *v = [cell.contentView viewWithTag:10];
    [v removeFromSuperview];

    cell.textLabel.text = @"some text";

     if (indexPath.section == 7) 
     { 
        if (indexPath.row == 1) 
        { 
            cell.detailTextLabel.text = @""; 
            MPVolumeView *systemVolumeSlider = [[MPVolumeView alloc] initWithFrame:CGRectMake(100, 10, 200, 100)];
            //set a tag so we can easily find it (to remove it)...
            systemVolumeSlider.tag = 10;  
            [cell.contentView addSubview:systemVolumeSlider]; 
            [systemVolumeSlider release]; 
            return cell; 
        }
     }

    cell.detailTextLabel.text = @"detail";

    return cell;
}

あなたのコメントでは、ボリューム コントロールは 8 番目のセクションの 2 行目にのみある必要があるように思われるため、例はそのように記述されています。必要に応じて変更します。

于 2010-10-26T00:45:34.970 に答える