我正在解析一个包含图片链接的 XML 文件,并尝试在 UITableView 中显示它们。由于某种原因,它们没有出现在 View 中。我认为这个问题与我的解析脚本没有任何关系,因为它可以在表格中显示文本。这是我显示图片的代码:
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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?
感谢您的帮助
安托万
Best Answer-推荐答案 strong>
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}
关于ios - 解析 XML - 从 URL 加载图像,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/14438442/
|