0

i am using the code below to implement a volume view into a cell.

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

However I have a problem with it. Whenever i scroll up or down in the tableview the MPVolumeView will be added to some other cells aswell. How could I fix this?


MKDev
  • 147
  • 2
  • 12
  • Why are you not adding it to cell.contentView instead of to cell and why are you adding cell to self.view? –  Oct 25 '10 at 01:32
  • I tried doing that but it is still appearing in other cells. – MKDev Oct 25 '10 at 10:39
  • If you only want it in certain cells, where is your if-condition to check whether to put in current cell or not? –  Oct 25 '10 at 13:49
  • if ([[cell textLabel].text isEqualToString:@"Volume"]) – MKDev Oct 25 '10 at 19:39
  • You should not use the cell itself to store data or "state". Either use the cell's row number (eg. if Volume is always going to be on row 4) or an array or dictionary that you can lookup using the cell's index path. This is because the cell could be reused when it scrolls out of view for another cell that scrolls into view. This also means you might have to remove the slider from the cell's subviews if the current cell is not "Volume" (the slider might be left over from an out-of-view "Volume" cell that is now being re-used). –  Oct 25 '10 at 20:25
  • Im trying this but it still wont work, other cells still have the volume view on them. if (indexPath.section == 7) { if (indexPath.row == 1) { cell.detailTextLabel.text = @""; MPVolumeView *systemVolumeSlider = [[MPVolumeView alloc] initWithFrame:CGRectMake(100, 10, 200, 100)]; [cell.contentView addSubview:systemVolumeSlider]; NSLog(@"being released"); [systemVolumeSlider release]; NSLog(@"been released"); } return cell; } – MKDev Oct 25 '10 at 23:17

1 Answers1

0

As mentioned in the comments, the cell with the Volume control may get re-used for non-Volume cells so it needs to be removed if it already exists. An example of how this can be done:

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

In your comments, it seems the volume control should only be on the 2nd row of the 8th section so the example is written that way. Modify as needed.