【问题标题】:How to remove UITableViewCell's subviews如何删除 UITableViewCell 的子视图
【发布时间】:2012-12-13 08:48:19
【问题描述】:

我创建了 imageview 以在 cellForRowAtIndexPath 方法上显示图像。我在 Cell 中添加 imageview 作为子视图。但是当我当时重新加载我的表格视图时,单元格中的图像不会重新加载。以下是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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

    imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[arrcathatname objectAtIndex:indexPath.row]]];
    [imgView setFrame:CGRectMake(20, 7, 35, 35)];
    [cell addSubview:imgView];

    cell.textLabel.text = [arrCatName objectAtIndex:indexPath.row];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Configure the cell...

    return cell;
}

所以,我想删除我的 imageview 以正确重新加载图像。

我该怎么做?

【问题讨论】:

  • 将子视图添加到 cell 或 cell.contentView??
  • 尝试使用 [cell.contentView addSubview:imgView];
  • 您是从服务器 url 获取图像吗?

标签: iphone objective-c uitableview


【解决方案1】:

尝试将子视图添加到单元格:

[cell.contentView addSubview:imgView];

您可以使用代码删除该子视图:

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

【讨论】:

    【解决方案2】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    imgView = [[UIImageView alloc] init];
    imgView.tag  = 111;
    
    
    
    [cell.contentView addSubView:imgView];
    }
    imgView = (UIImageView*)[cell.contentView ViewWithTag:111];
    
    [imgView setFrame:CGRectMake(20, 7, 35, 35)];
    imgView.image = [UIImage imageNamed:[arrcathatname objectAtIndex:indexPath.row]];
    
    
    cell.textLabel.text = [arrCatName objectAtIndex:indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Configure the cell...
    
    return cell;
    }
    

    试试这个

    【讨论】:

    • 你试过这个我已经更新了再次请检查。如果您还有其他问题,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    相关资源
    最近更新 更多