我正在解析一个包含图片链接的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后只得到图像的URL?
感谢任何帮助 安托
答案 0 :(得分:2)
- (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;
}
答案 1 :(得分:1)
您需要在表视图中异步加载图像,请按照本教程的帮助进行操作
http://kosmaczewski.net/asynchronous-loading-of-images-in-a-uitableview/
答案 2 :(得分:1)
@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];
}