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を取得するので、どうすればそれを行うことができますか?

助けを求めるThxAntoine

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 に答える