0

我有一个以下列方式加载的表格视图。如果满足条件,我需要在cell.imageview. 图像也有不同的尺寸。下面是我的代码,任何人都可以指出我哪里出错了。

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if(array==nil||[array count]==0)
{

}
else
{
    NSMutableDictionary *dicttable=[array objectAtIndex:indexPath.row];
    NSString *head=[dicttable objectForKey:@"name"];
    NSString *type=[dicttable objectForKey:@"type"];

    NSString *imgUrl = [dicttable objectForKey:@"image"];;
    if(imgUrl!=nil)
    {
        if(![[ImageCache sharedImageCache] hasImageWithKey:imgUrl])
        { 
            cell.imageView.image = [UIImage imageNamed:@"noimage_icon.png"];
            NSArray *myArray = [NSArray arrayWithObjects:cell.imageView,imgUrl,@"noimage_icon.png",[NSNumber numberWithBool:NO],nil];
            AppDelegate *appDelegate = (AppDelegate  *)[[UIApplication sharedApplication] delegate];
            [appDelegate performSelectorInBackground:@selector(updateImageViewInBackground:) withObject:myArray];
            cell.imageView.frame=CGRectMake(0,0,48,48);
            cell.imageView.bounds=CGRectMake(0,0,48,48);
            [cell.imageView setClipsToBounds:NO];
        }
        else
        {
            cell.imageView.image = [[ImageCache sharedImageCache] getImagefromCacheOrUrl:imgUrl];
            cell.imageView.frame=CGRectMake(0,0,48,48);
            cell.imageView.bounds=CGRectMake(0,0,48,48);
            [cell.imageView setClipsToBounds:NO];
        }
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"noimage_icon.png"];
    }
    if([type isEqualToString:@"YES"])
    {
        UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bluel.png"]];
        [cell setBackgroundView:img];
        [img release];

        cell.textLabel.text = head;
        cell.textLabel.backgroundColor = [UIColor clearColor];

        cell.detailTextLabel.textColor=[UIColor grayColor];
        cell.detailTextLabel.text = subtitle1;
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    }
    else
    {
        UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wnew1.png"]];
        [cell setBackgroundView:img];
        [img release];
        cell.textLabel.text = head;
        cell.textLabel.backgroundColor = [UIColor clearColor];
        [cell.imageView addsubview:spclimage]

        cell.textLabel.text = head;
        cell.textLabel.backgroundColor = [UIColor clearColor];

        cell.detailTextLabel.textColor=[UIColor grayColor];
        cell.detailTextLabel.text = subtitle1;
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];            
    }
}
return cell;

这里的问题仅在特殊图像添加的最后一行。不是在所有行中。此外,在重新加载表格视图时,图像视图大小也不同?

4

1 回答 1

1

几个想法:

  1. updateImageViewInBackground似乎很可疑,顾名思义,您正在更新图像视图,但您没有指定要更新的单元格。

  2. 我也看到你在做一个addSubview:spclimage. 显然,如果那spclimage是在另一个单元格上,一旦你这样做addSubview,它将从以前的位置删除,然后再添加到当前单元格中。事实上,只是将图像添加为现有图像视图的子视图的想法很奇怪。

  3. 如果您的缓存还没有图像,我会看到您使用 初始化图像的位置noimage_icon.png,但看不到您实际更新图像视图的位置。你说updateImageViewInBackground的是“然后更新图像”。您的意思是为this设置image属性吗?或者也许更新?如果是这样,那就有问题了。imageViewcellindexPathspclimage

  4. 典型的模式(使用GCD)是:

    cell.imageView.image = [UIImage imageNamed:@"noimage_icon.png"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = ... // do whatever you need to do to get the image, load cache, etc.
    
        // ok, now that you have the image, dispatch the update of the UI back to the main queue
    
        dispatch_async(dispatch_get_main_queue(), ^{
    
            // because we're doing this asynchronously, make sure the cell is still
            // visible (it could have scrolled off and the cell was dequeued and
            // reused), so we're going to ask the tableview for the cell for that
            // indexPath, and it returns `nil` if it's not visible. This method is
            // not to be confused with the similarly named `UITableViewControllerDelegate`
            // method.
    
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
            // if the image view is still visible, update it
    
            if (cell)
            {
                cell.imageView.image = image;
            }    
        });
    
    });
    
于 2013-01-03T15:08:20.480 回答