2011-12-14 20 views
0

XMLフィードの表を配列にロードしてから、以下のコードでセルを作成しています。ときどきロードすると本当にぎくしゃくすることがあります。スクロールすると画像がダウンロードされただけだと思っていましたが、テストのために削除してしまいました。私が間違っていることを解決できません!スクロール時にUITableViewジャケが発生する

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    const NSInteger BOTTOM_LABEL_TAG = 1002; 
    const NSInteger TEXT_FIELD_TAG = 1003; 

    UILabel *Labelcompanyname; 
    UILabel *Labeldistance; 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 

     cell = 
     [[[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier] 
     autorelease]; 


     const CGFloat LABEL_HEIGHT = 15; 


     int spaceSize; 

     if(currentType == @"categorySearch"){ 

       spaceSize = 57; 
     } 
     else { 

       spaceSize = 34; 
     } 


     Labelcompanyname = 
     [[[UILabel alloc] 
      initWithFrame: 
      CGRectMake(
        spaceSize + 2.0 * cell.indentationWidth, 
        0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT)+10, 
        tableView.bounds.size.width - 
        spaceSize - 20.0, 
        LABEL_HEIGHT)] 
     autorelease]; 
     [cell.contentView addSubview:Labelcompanyname]; 


     Labeldistance =[[[UILabel alloc] 
         initWithFrame: 
         CGRectMake(
            spaceSize + 2.0 * cell.indentationWidth, 
            0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT)+40, 
            tableView.bounds.size.width - 
            spaceSize - 20.0, 
            LABEL_HEIGHT)] 
         autorelease]; 
     if(currentType == @"categorySearch") { 
      [cell.contentView addSubview:Labeldistance]; 

     } 


     Labelcompanyname.tag = BOTTOM_LABEL_TAG; 
     Labelcompanyname.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2]; 

     Labeldistance.tag = TEXT_FIELD_TAG; 
     Labeldistance.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2]; 

     cell.backgroundView = [[[UIImageView alloc] init] autorelease]; 
     cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease]; 
     cell.textLabel.numberOfLines=0; 

    } 
    else 
    { 
     Labelcompanyname = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG]; 
     Labeldistance = (UILabel *)[cell viewWithTag:TEXT_FIELD_TAG]; 

    } 

    PromotionObject *newObj1; 
    newObj1=[totalArray objectAtIndex:indexPath.row]; 

    if(!newObj1.furtherURL){ 

     Labelcompanyname.text =[NSString stringWithFormat:@"%@", newObj1.companyname]; 
     Labeldistance.text =newObj1.distance; 
    } 
    else 
    { 
     Labelcompanyname.text =[NSString stringWithFormat:@"Load more results"]; 
     Labeldistance.text [email protected]""; 
    } 



    NSURL *Imageurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", newObj1.locImage]]; 
    NSData *data = [NSData dataWithContentsOfURL:Imageurl]; 
    UIImage *img = [[[UIImage alloc] initWithData:data] autorelease]; 

    if(currentType == @"categorySearch"){ 

     cell.imageView.image = img; 

    } else if(currentType == @"fullSearch") { 

     cell.imageView.image = [UIImage imageNamed:@"newmap.png"]; 

    } else { 

     cell.imageView.image = [UIImage imageNamed:@"business.png"]; 

    } 


    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

すべてのヘルプは本当に細胞をロード中にあなたは間違いなくかなりの時間のためにブロックUIスレッド何もしてはならない

トム

答えて

3

を理解されるであろう。例えば。 NSData *data = [NSData dataWithContentsOfURL:Imageurl];

データをバックグラウンドで読み込み、データがダウンロードされたときにテーブル/セルを再読み込み/更新することをお勧めします。また、画像のキャッシュを実装するのに適しているので、セルが読み込まれるたびに再度ダウンロードする必要はありません。

非同期イメージの読み込みに関するヘルプが必要な場合は、そのイメージを検索してください。あなたのものはたくさんあります。

+1

http://afnetworking.orgのimageViewカテゴリを使用している場合、これはすべてあなたのために処理されます – adamweeks

+0

ありがとうございました – TMB87

+0

+1のためにafnetworking.org非常に見栄えが良く、非常に便利です。 –

1

あなた自身があなたの答えを持っていると思います。イメージの読み込みによって、UIが現在のセルリクエストを終了できなくなります。 tableView:cellForRowAtIndexPath:イメージが取得されるまで返ることはできません。画像をアクティビティインジケータで置き換え、バックグラウンドで画像を取り込むことをお勧めします。 Concurrency Programming Guide

関連する問題