我有一个以下面的方式加载的表格 View 。如果满足条件,我需要在 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;
这里的问题仅在于添加特殊图像的最后一行。并非在所有行中。另外,在重新加载表格 View 的过程中, ImageView 的大小也有所不同?
最佳答案
一些想法:
<updateImageViewInBackground
似乎很可疑,顾名思义,您正在更新 ImageView ,但您没有指定要更新哪个单元格。我还看到您在执行
addSubview:spclimage
。显然,如果该 spclimage 位于另一个单元格上,那么一旦您执行 addSubview ,它将在添加到当前单元格之前从之前的位置删除。事实上,将图像添加为现有 ImageView 的 subview 的想法就很奇怪。如果您的缓存还没有图像,我会看到您使用
noimage_icon.png
初始化图像的位置,但看不到您实际更新的位置 ImageView 。你说 updateImageViewInBackground 是“然后更新图像”。您的意思是为此indexPath
设置此cell
的imageView
的image
属性吗?或者也许更新spclimage
?如果是这样,那就有问题了。典型的模式(使用
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; } }); });
关于iphone - 我的 ImageView 在 TableView 中表现不正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14140822/