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