2010-10-24 33 views
0

我正在使用下面的代碼來實現一個單元的體積視圖。MPVolumeView出現在其他單元中

[[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將被添加到其他一些單元格中。我怎麼能解決這個問題?


+0

爲什麼你不把它添加到cell.contentView而不是單元格,爲什麼要添加單元格到self.view? – Anna 2010-10-25 01:32:53

+0

我試過這樣做,但它仍然出現在其他單元格中。 – MKDev 2010-10-25 10:39:32

+0

如果你只想在特定的單元格中,你的if條件在哪裏檢查是否放入當前單元格? – Anna 2010-10-25 13:49:54

回答

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行這樣的例子寫的方式。根據需要修改。

相關問題