3

カスタム cellがありUITableViewます。カスタム セルには. ここに画像が非同期で読み込まれます。しかし、テーブル ビューをスクロールすると、セル内の画像が変わります。これが私のコードです:UIView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    MSAppointmentsModel *data = [self.appointments objectAtIndex:indexPath.row];

    MSAppointmentsCell *cell = (MSAppointmentsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *ar = [[NSBundle mainBundle] loadNibNamed:[Common checkDeviceforController:@"MSAppointmentsCell"] owner:nil options:nil];
        for (id eachObject in ar) {
            if ([eachObject isKindOfClass:[UITableViewCell class]]) {
                cell = eachObject;
            }
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSURL *imgUrl = [NSURL URLWithString:[data.cusDetails objectForKey:@"customer_image"]];
            NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
            UIImage *image = [UIImage imageWithData:imgData];
            dispatch_sync(dispatch_get_main_queue(), ^{
                [cell.medallionView setImage:image];
            });
        });

        if (data.comp_status == YES) {
            cell.status.image = [Common imageResize:[UIImage imageNamed:@"CheckMark_green.png"] andResizeTo:cell.status.frame.size];
        }

    }

    cell.custName.text = [data.cusDetails objectForKey:@"customer_name"];
    cell.service.text = [data service];
    cell.empName.text = [NSString stringWithFormat:@"by %@",[data.empDetails objectForKey:@"employee_name"]];


    return cell;
}

私を助けてください。

4

3 に答える 3

4

これを試して、

1: イメージをキャッシュするための配列を取得 NSMutablearray * imageicons;

2: データをダウンロードした後、配列に NULL 値を追加します。

 imageicons=[[NSMutableArray alloc] init];

 for (int i=0; i<[self.appointments count]; i++) 
{
 [imageicons insertObject:[NSNull null] atIndex:i];
}

3:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(indexPath.row < [imageicons count] && [imageicons objectAtIndex:indexPath.row] != [NSNull null] )
        {
            [cell.bookimage setImage:[imageicons objectAtIndex:indexPath.row]];
            NSLog(@"Array Hit");

        }
        else{
        NSLog(@"Count of %i Size of %li Array and row number %i",[imageicons count],sizeof(imageicons),indexPath.row);
      [NSThread detachNewThreadSelector:@selector(DownloadImageInBackground:) toTarget:self withObject:indexPath];
        }
 }

-(void)DownloadImageInBackground:(NSIndexPath *)path
{
MSAppointmentsModel *data = [self.appointments objectAtIndex:indexPath.row];
NSString *str=[data.cusDetails objectForKey:@"customer_image"];

  imagetodisplay  = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];

if (imagetodisplay==Nil)
{

  // set deault image
     [imageicons replaceObjectAtIndex:path.row withObject:[UIImage imageNamed:@"defaultimg.png"]];
}
else
{

    MSAppointmentsCell *cell=(MSAppointmentsCell *)[self.booktableview cellForRowAtIndexPath:path];
    [cell.bookimage setImage:imagetodisplay];
    //NSLog(@"Count of %i Size of %li Array and row number %i",[imageicons count],sizeof(imageicons),path.row);

    [imageicons replaceObjectAtIndex:path.row withObject:imagetodisplay];


}

}
于 2013-07-25T12:02:24.873 に答える
0

メダリオンビューが画像を保持していると思いますので、いくつか問題があります。

セルをデキューするため、エラーが発生しますが、画像ビュー (medallionView) ではなく、custName、service、および empName ラベルのテキストを設定するだけではありません。したがって、基本的には、(画面サイズ/セルの高さ)行数(例9)を描画し、それらのセルにデータを入力してから、そこにある画像を使用してそれらを再利用し続けます。

また、非同期リクエストを行ったとき、非同期リクエスト内に決して描画しないように言われました。代わりに、URL からすべての画像を取得して配列に格納し、すべてがロードされたら、1) tableView デリゲート クラスに通知して、データまたは 2) データがロードされた後に実行されるデリゲート メソッドを持っているか、または 3) すべての画像がロードされた後に実行される完了ブロックを持っています。

于 2013-07-25T12:02:17.943 に答える
0

setNeedsDisplay後に imageView に呼び出しを追加してみてくださいcell.empName.text = ...

すみません、何か見落としていました。更新しました。

于 2013-07-25T12:01:03.087 に答える