如何停止重用uitableviewCell?

时间:2013-09-30 13:13:46

标签: ios uitableview lazy-loading

嗨,在我的应用程序中,我必须将图像加载到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;
 }

2 个答案:

答案 0 :(得分:0)

您无法在默认表格视图单元格中执行此操作。如果你想创建这类东西,那么你需要创建自定义的 tableview 单元格并按照你想要的那样做。

答案 1 :(得分: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
<\ n>在IB之后,您在UITableViewCell添加了一个新的UITableView,并在其中添加了新的自定义单元格,将识别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;