【问题标题】:UIImageView in UITableView cell content view is not updatingUITableView 单元格内容视图中的 UIImageView 未更新
【发布时间】:2012-08-07 13:14:45
【问题描述】:

在我的 iPhone 应用程序中,我有一个 UITableView,它包含一个 UIImageView、按钮和标签。我正在根据来自服务器的值更新我的数据库并更新表格视图中的详细信息。如果我第二次运行该应用程序,在服务器上进行一些修改后,图像视图没有得到更新。按钮和标签正在更新。当我从数据库中检查图像的本地路径时,它会在文档文件夹中显示新图像,但表格单元格仍显示旧图像。要查看更新的图像,我应该重新安装该应用程序。我应该怎么做才能解决这个问题?

这是我所做的工作流程:

  1. 从数据库中加载新值,并将所有值保存在一个数组中
  2. 删除表格视图
  3. 再次创建表格视图
  4. 重新加载表格视图。

编辑 //为表格视图创建自定义单元格以显示不同的对象

- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 150, 60);
    CGRect Label1Frame = CGRectMake(20, 23, 98, 30);
    CGRect imgFrame = CGRectMake(20, 48, 110, 123);
    CGRect btnFrame = CGRectMake(25, 136, 100, 30);

    UILabel *lblTemp;
    UIImageView *itemImg;
    UIButton *itemBtn;

    UIMenuItemCell *cell = [[UIMenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.frame = CellFrame;

    //Initialize Label with tag 1.  
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    lblTemp.tag = 1;
    lblTemp.textColor=[UIColor colorWithRed:139.0f/255.0f green:69.0f/255.0f blue:19.0f/255.0f alpha:1.0f];
    lblTemp.textAlignment = UITextAlignmentCenter;
    lblTemp.backgroundColor = [UIColor clearColor];
    lblTemp.font = [UIFont systemFontOfSize:13.0];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];

    //Initialize ImageView
    itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
    itemImg.tag = 2;
    [cell.contentView addSubview:itemImg];
    [itemImg release];

    //Initialize Button
    itemBtn = [[UIButton alloc]initWithFrame:btnFrame];
    itemBtn.frame = btnFrame;
    itemBtn.tag = 3;
    itemBtn.titleLabel.textColor = [UIColor blueColor];
    itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
    [cell.contentView addSubview:itemBtn];
    [itemBtn release];


    return cell;
}

// 自定义表格视图单元格的外观。

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

        NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];

        UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


        if(cell == nil){
            cell = [self getCellContentView:CellIdentifier];

            cell.transform = CGAffineTransformMakeRotation(M_PI_2);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.cellItemName = (UILabel *)[cell viewWithTag:1];
            cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
            cell.cellItemButton = (UIButton *)[cell viewWithTag:3];

            DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];

            NSString *imageLocalFilePath = nil;
            if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:@"NotAvailable"]) {
                cell.cellItemProgress.hidden = YES;
                cell.cellItemButton.hidden = NO;
                imageLocalFilePath = [NSString stringWithFormat:@"%@",[tempItemLocalNotAvailPath objectAtIndex:indexPath.row]];
                NSString *date = [self changeDateFormat:itemObj.itemReleaseDate];          
                [cell.cellItemButton setTitle:date forState:UIControlStateNormal]; 
                cell.cellItemButton.userInteractionEnabled = NO;
                cell.userInteractionEnabled = NO;
                [cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
                [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"not_available_bttn_bck_img"] forState:UIControlStateNormal];
            }else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:@"Available"]){
                cell.cellItemButton.userInteractionEnabled = YES;
                cell.userInteractionEnabled = YES;
                cell.cellItemProgress.hidden = YES;
                [cell.cellItemButton setTitle:@"" forState:UIControlStateNormal];
                [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"available_bttn_img_normal"] forState:UIControlStateNormal];
                [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"available_bttn_img_pressed"] forState:UIControlStateHighlighted];
                [cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
                [cell.cellItemButton addTarget:self action:@selector(confirmationAlert:) forControlEvents:UIControlEventTouchUpInside];
                imageLocalFilePath = [NSString stringWithFormat:@"%@",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
            }
            if ([imageLocalFilePath isEqualToString:@""]) {
                [cell.cellitemImage setImage:[UIImage imageNamed:@"item01.png"]];
            }else {
                [cell.cellitemImage setImageWithURL:[NSURL fileURLWithPath:imageLocalFilePath] placeholderImage:[UIImage imageNamed:@"item01.png"]];
            }        
            cell.cellItemName.text = [NSString stringWithFormat:@"%@",[tempItemNameArray objectAtIndex:indexPath.row]];
        }

        return cell;
    }

请帮忙。

【问题讨论】:

  • 您是否在重复使用 UITableViewCells 而没有正确更新 cellForRowAtIndexPath 中的图像?
  • 是的,我正在从数据库中获取最新的详细信息并将其加载到数组中。这些值将被加载到表中。问题仅在于图像视图,所有其他对象都很好。当我检查时,我正在从数据库中获取最新的图像路径,其中包含新的内容,但仍然显示旧的。
  • 你能显示你的tableView代码吗:cellForRowAtIndexPath:
  • 我已添加代码,请检查。
  • 你在哪里关闭 if(cell == nil) ?确保设置所有数据,即使单元格存在 (!=nil)。

标签: iphone uitableview uiimageview


【解决方案1】:

我发现了问题,在cellForRowAtIndexPath方法中,设置图像,我使用了代码

[cell.cellitemImage setImageWithURL:[NSURL fileURLWithPath:imageLocalFilePath] placeholderImage:[UIImage imageNamed:@"item01.png"]];

我使用外部类调用ImageLoading 来加载图像,并且该类包含一些缓存方法,这导致了问题。所以我把那行改成

[cell.cellitemImage setImage:[UIImage imageWithContentsOfFile:imageLocalFilePath]];

解决了这个问题。

感谢您的支持:)

【讨论】:

    【解决方案2】:

    应该有一个 }

    行后

    cell.cellItemButton = (UIButton *)[cell viewWithTag:3]; 
    

    【讨论】:

    • 对不起,这是在代码末尾,我在复制时错过了。现已更新。
    【解决方案3】:

    我先看了你的代码,发现了这个“错误”:

       NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];
    

    所以...它不会给您任何错误,它可以工作...

    但这与表格的工作方式背道而驰: 这样做你正在为你的数组/数据库的每个项目创建和分配一个新的单元格, 这意味着如果您需要滚动查看 1.000 个项目,则需要创建 1.000 个单元格!!!

    不应该是这样的。

    通常,表格只创建需要在屏幕/显示器上显示的单元格

    表示如果您的表格区域高度为 300 像素且每个单元格高度为 30 像素, 那么您可能只需要 11 个单元,并且您应该只使用/分配 11 个单元,而不是 1.000

    单元格的神奇之处在于当用户将单元格滚动出屏幕(例如向上)时重复使用单元格,将其数据和图像设置为新的项目数据并再次将其放在屏幕上(例如向下)

    这就是 CellIdentifier 通常的用途,并且对于表格中的所有单元格应该是相同的(至少如果所有单元格都相似,但包含的数据,并且您不需要不同的单元格)

    如果您管理的项目过多,分配过多的单元格可能会给您带来记忆问题...

    附言

    我可能没有回答您的问题,您的代码中可能还有其他问题,正如我所说的我只是匆忙阅读它

    【讨论】:

    • 滚动表格时我需要保留项目的一些状态。当我按照你说的做时,它不起作用,所以我被告知要做这样的事情,它解决了这个问题。
    【解决方案4】:

    您必须在从数据库中加载数据和重新加载表格视图之间保留一些时间延迟。然后您将能够看到更新后的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多