Iphone 我的ImageView在表视图中运行不正常?
我有一个以下面的方式加载的表视图。如果满足条件,我需要在Iphone 我的ImageView在表视图中运行不正常?,iphone,ios,ipad,Iphone,Ios,Ipad,我有一个以下面的方式加载的表视图。如果满足条件,我需要在单元格上方添加特定图像。imageview。此外,这些图像来自不同的维度。下面是我的代码,谁能告诉我哪里出了问题 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UI
单元格上方添加特定图像。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;
这里的问题只出现在特殊图像添加的最后一行。不是所有的行。此外,在重新加载tableview的所有时间内,图像视图的大小都是不同的?一些想法:
updateImageViewInBackground
似乎是可疑的,因为名称表明您正在更新图像视图,但您没有指定要更新的单元格addSubview:spclimage
。显然,如果该spclimage
位于另一个单元格上,则只要执行addSubview
,它就会在添加到当前单元格之前从上一个位置删除。事实上,仅仅是将图像添加为现有imageview的子视图的概念就很奇怪了noimage_icon.png
初始化图像,但我看不到您在哪里实际更新图像视图。你说的updateImageViewInBackground
是“然后更新图像”。您的意思是为该单元格的图像视图设置属性吗?或者更新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;
}
});
});
这是一种图像缓存方法,我们在其中缓存图像。仅此而已。那里没有发生更多的事情。@Rob是一种appdelegate方法,我们将图像存储到缓存中,然后更新图像。@Rob非常感谢您最后的宝贵评论。因此,如果我需要将该特殊图像添加到所有单元格中,您可以帮助我如何操作吗?我已经完成了补充了一个答案。典型的模式是,您调用一个方法,该方法知道要为哪个
indepath
检索图像,它将检索该图像,然后检查该indepath
的单元格是否仍在屏幕上,如果是,则相应地更新图像。(顺便说一句,您将在S.O.上看到的许多tableview异步图像加载过程忽略了检查以确保单元格仍然可见。这是一个关键步骤。)@Rob非常感谢您的时间。但我的问题仍然是如何将图像添加到现有的cellimageview。正如您正确地说的,在一个单元格中使用addsubview时,它将从另一个单元格中删除。