0

嗨,在我的应用程序中,我必须将图像加载到UITableViewCell来自服务器的图像。所以问题是每当我滚动时,tableview 图像每次都在加载并且它的加载很懒,所以我试图停止重用单元格,但它不起作用,如何停止重复使用中的单元格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

你必须像这样制作一个自定义单元格:

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和它的类之后,新的自定义单元格设置识别 ID,如“RestaurantCustomCell”,将标签和 imageView 添加到自定义单元格并连接 Outlet,然后你 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 回答
0

您不能在默认表格视图单元格中执行此操作。如果你想创建这种东西,那么你需要创建自定义的tableview单元,然后按照你的意愿去做。

于 2013-09-30T13:51:33.623 回答