0

こんにちは、私のアプリでUITableViewCellは、サーバーから来る画像をロードする必要があります。問題は、テーブルビューをスクロールするたびに画像がロードされ、ロードが遅延しているため、セルの再利用を停止しようとしましたが、機能しません。のセルの再利用を停止しUITableViewます。私のコードは

     -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *CellIdentifier=@"Cell";
RestaurantCustomCell *cell =(RestaurantCustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects ;
topLevelObjects= [[NSArray alloc]init];
if(cell==nil)
{
        topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RestaurantCustomCell" owner:self options:nil];
    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[UITableViewCell class]])
        {
            cell = (RestaurantCustomCell *) currentObject;
            break;
        }
    }
}
 NSDictionary *dict=[restauarantsArr objectAtIndex:indexPath.row];
NSString *imgStr=[dict valueForKey:@"Img"];
[self processImageDataWithURLString:imgStr andBlock:^(NSData *imageData) {
    if (self.view.window)
    {
        UIImage *img = [UIImage imageWithData:imageData];
        if(img!=nil)
        {
            UIGraphicsBeginImageContext(CGSizeMake(100, 100));
            [img drawInRect:CGRectMake(0,0,100,100)];
            UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
            cell.restaurantImg.image=newImage;
            UIGraphicsEndImageContext();
        }
    }
}];

cell.nameLbl.text=[dict valueForKey:@"Restaurants"];
cell.typeLbl.text=[dict valueForKey:@"Rest_Cuisine"];
cell.timingsLbl.text=[dict valueForKey:@"Rest_Timings"];
cell.categoryLbl.text=[dict valueForKey:@"Rest_Category"];
cell.addressLbl.text=[dict valueForKey:@"Address"];

return cell;
 }
4

2 に答える 2

0

デフォルトのテーブルビュー cellではこれを行うことはできません。このようなものを作成したい場合は、カスタムのテーブルビューセルを作成し、必要に応じて実行する必要があります。

于 2013-09-30T13:51:33.623 に答える
0

そのようなカスタムセルを作成する必要があります:

RestaurantCustomCell.h

#import <UIKit/UIKit.h>

@interface RestaurantCustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *nameLbl;
@property (weak, nonatomic) IBOutlet UILabel *typeLbl;
@property (weak, nonatomic) IBOutlet UILabel *timingsLbl;
@property (weak, nonatomic) IBOutlet UILabel *categoryLbl;
@property (weak, nonatomic) IBOutlet UILabel *addressLbl;
@property (weak, nonatomic) IBOutlet UIImageView *restauranImg;


@end

RestaurantCustomCell.m

#import "RestaurantCustomCell.h"

@implementation RestaurantCustomCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)prepareForReuse{
    self.nameLbl.text = @"";
    self.typeLbl.text = @"";
    self.timingsLbl.text = @"";
    self.categoryLbl.text = @"";
    self.addressLbl.text = @"";
}

@end

IBで新しいものを追加しUITableViewCellUITableViewそのクラスを新しいカスタムセルで設定した後、「RestaurantCustomCell」のような識別IDを設定して、ラベルとimageViewをカスタムセルに追加し、アウトレットを接続してから、次の tableView:cellForRowAtIndexPath:ように変更します。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *CellIdentifier=@"RestaurantCustomCell";
RestaurantCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

 NSDictionary *dict=[restauarantsArr objectAtIndex:indexPath.row];
NSString *imgStr=[dict valueForKey:@"Img"];
[self processImageDataWithURLString:imgStr andBlock:^(NSData *imageData) {
    if (self.view.window)
    {
        UIImage *img = [UIImage imageWithData:imageData];
        if(img!=nil)
        {
            UIGraphicsBeginImageContext(CGSizeMake(100, 100));
            [img drawInRect:CGRectMake(0,0,100,100)];
            UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
            cell.restaurantImg.image=newImage;
            UIGraphicsEndImageContext();
        }
    }
}];

cell.nameLbl.text=[dict valueForKey:@"Restaurants"];
cell.typeLbl.text=[dict valueForKey:@"Rest_Cuisine"];
cell.timingsLbl.text=[dict valueForKey:@"Rest_Timings"];
cell.categoryLbl.text=[dict valueForKey:@"Rest_Category"];
cell.addressLbl.text=[dict valueForKey:@"Address"];

return cell;
于 2013-09-30T14:20:12.390 に答える