我正在解析一个包含图片链接的XML文件,并尝试在UITableView中显示它们。由于某种原因,它们没有出现在视图中。我不认为这个问题与我的解析脚本有任何关系,因为它可以在表中显示文本。下面是我显示图片的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
UIImageView *customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.tag = 0013;
[cell.contentView addSubview:customImage];
}
UIImageView *customImage = (UIImageView *)[cell.contentView viewWithTag:0013];
customImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentTweet pic]]]];
return cell;
}
我可能需要先加载图片吗?如果是这样的话,我如何才能做到这一点,因为我只在解析xml之后才获得图像的urls?
感谢任何帮助,Antoine
发布于 2013-01-21 20:42:36
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *customImage = (UIImageView*)[cell.contentView viewWithTag:1234];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.tag = 1234;
[cell.contentView addSubview:customImage];
}
dispatch_queue_t imageQueue = dispatch_queue_create("queue", NULL);
dispatch_async(imageQueue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentTweet pic]]]];
dispatch_async(dispatch_get_main_queue(), ^{
customImage.image = image;
});
});
return cell;
}
发布于 2013-01-21 20:24:04
您需要在表视图中异步加载图像,遵循本教程将有所帮助
http://dbrajkovic.wordpress.com/2012/01/08/load-images-asynchronously-in-a-uitableview-using-gcd-grand-central-dispatch/
http://kosmaczewski.net/asynchronous-loading-of-images-in-a-uitableview/
发布于 2013-01-21 20:35:25
@interface ViewController ()
{
NSMutableArray *images;
NSMutableDictionary *dicImages_msg;
BOOL isDecliring_msg;
BOOL isDragging_msg;
}
@implementation ViewController ()
- (void)viewDidLoad
{
[super viewDidLoad];
dicImages_msg = [[NSMutableDictionary alloc] init];
[self parsing];//parse and save the images to the images array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Set up the cell...
}
cell.imageView.image=[UIImage imageNamed:@"placeholder.png"]; //placeholder image is blank image displayed till the image loads
if ([dicImages_msg valueForKey:[images objectAtIndex:indexPath.row]])
{
if (images.count == 0)
{
cell.imageView.image=[UIImage imageNamed:@"placeholder.png"];
}
else
{
cell.imageView.image=[dicImages_msg valueForKey:[images objectAtIndex:indexPath.row]];
}
}
else
{
if (!isDragging_msg && !isDecliring_msg)
{
[self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath];
}
else
{
cell.imageView.image=[UIImage imageNamed:@"placeholder.png"];
}
}
return cell;
}
-(void)downloadImage_3:(NSIndexPath *)path
{
NSAutoreleasePool *pl = [[NSAutoreleasePool alloc] init];
UIImage *img=[[UIImage alloc]init];
img=[UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:path.row]]]];
[dicImages_msg setObject:img forKey:[images objectAtIndex:path.row]];
[self.YourTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[pl release];
}
https://stackoverflow.com/questions/14438442
复制相似问题