自定义单元格实现问题中的NSMutableArray

时间:2014-06-27 05:33:58

标签: ios uitableview nsmutablearray

所以我在SpecialOfferCell.h上有这个代码

#import <UIKit/UIKit.h>
#import "AFImagePager.h"
@interface SpecialOfferCell : UITableViewCell<AFImagePagerDataSource,AFImagePagerDelegate>
@property (nonatomic, retain) NSMutableArray *arrayImages;
@property (nonatomic) NSInteger offer_id;
-(void)loadImages;
@property (weak, nonatomic) IBOutlet AFImagePager *imageIcons;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;

@end

这是实施文件

#import "SpecialOfferCell.h"

@implementation SpecialOfferCell
@synthesize arrayImages;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

}
return self;
}

 - (void)awakeFromNib
 {
// Initialization code
self.imageIcons.delegate=self;
self.imageIcons.dataSource=self;


}


 -(NSArray*)getArrayImages{
 return  [NSArray arrayWithArray:arrayImages];
 }
#pragma mark - AFImagePager DataSource
- (NSArray *) arrayWithImageUrlStrings
{
 return [self getArrayImages];
}

- (UIViewContentMode) contentModeForImage:(NSUInteger)image
{
  return UIViewContentModeScaleAspectFill;
 }
- (UIImage *) placeHolderImageForImagePager{
  return [UIImage imageNamed:@"stub_dashboard"];
}
 -(void)loadImages{
NSLog(@"offer id :%d",self.offer_id);
NSLog(@"arrayImages:%d",arrayImages.count);
@try {
    if (arrayImages.count==0) {
        NSError* error;
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:[NSURL URLWithString:[appConfig URL_GET_OFFER_GALLERY]]];

        NSString *post = [NSString stringWithFormat:@"{\"hotel_id\":\"%@\",\"key\":\"%@\",\"offer_id\":%ld}",@"ayodya",[appConfig API_KEY],(long)_offer_id];
        NSLog(@"%@",post);
        NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];  // multipart/form-data
        [request setHTTPBody:postData];


        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        //NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        //NSLog(@"%@",returnString);
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
        arrayImages=[[NSMutableArray alloc]init];
        NSMutableArray *arrayImagesTemp=[[NSMutableArray alloc]init];
        arrayImagesTemp=[json objectForKey:@"galeries"];
        for (int i=0; i<arrayImagesTemp.count; i++) {
            NSDictionary * Object=[[NSDictionary alloc]init];
            Object=[arrayImagesTemp objectAtIndex:i];
            [arrayImages addObject:[Object objectForKey:@"image"]];
        }
        [self.imageIcons reloadData];

    }

}
@catch (NSException *exception) {

}
}

@end

因此,当我在UITableViewController中使用之前使用过的数组调用此自定义单元格时,所以当初始化新单元格时,数组不会刷新。这是为什么?是因为dequeueReusableCellWithIdentifier?

数据来源:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSDictionary * Object=[[NSDictionary alloc]init];
 Object=[array objectAtIndex:indexPath.row];
 static NSString *CellIdentifier = @"SpecialOfferCell";
 SpecialOfferCell *cell = (SpecialOfferCell *)[tableView
                                       dequeueReusableCellWithIdentifier:CellIdentifier];

 cell.titleLabel.text=[Object objectForKey:@"offer_title"];
 [cell setOffer_id:[[Object objectForKey:@"offer_id"]integerValue]];

 [cell performSelectorInBackground:@selector(loadImages) withObject:nil];

 return cell;
 }

所以我要在这里实现的是,当单元格加载图像时,它不会再次加载。 TableView

2 个答案:

答案 0 :(得分:0)

在您的SpecialOfferCell中,您需要在此处实现prepareForReuse,您可以重置任何数据,例如图像数组

答案 1 :(得分:0)

首先,这部分代码是无用的:

if (cell == nil)
{
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

你应该创建新的单元格而不是再次出列。

我不知道你真正想要实现的目标。有两种选择:

  1. prepareForReuse中,您可以使用loadImages删除图片以重新加载它们。
  2. 您不删除图像 - 在这种情况下,当重复使用单元格时,它会先前加载图像,因此基本上是随机的。
  3. 但是如果您只需要从服务器加载一次图像,您就可以创建字典,每次都在loadImages检查您的图像是否已经加载并从字典中获取。我理解对了吗?