MPVolumeView 出现在其他单元格中

发布于 2024-09-29 00:54:03 字数 412 浏览 0 评论 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 也会添加到其他一些单元格中。我该如何解决这个问题?


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?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-10-06 00:54:03

正如评论中提到的,带有音量控件的单元格可能会被非音量单元格重新使用,因此如果它已经存在,则需要将其删除。如何做到这一点的一个示例:

- (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 部分的第二行,因此示例就是这样编写的。根据需要修改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文