2

我正在解析一个包含图片链接的 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?

感谢任何帮助安托万

4

3 回答 3

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;
}
于 2013-01-21T12:42:35.660 回答
1

您需要在表格视图中异步加载图像,按照本教程将有所帮助

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-21T12:24:03.907 回答
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];

}
于 2013-01-21T12:35:25.450 回答