所以我對我的SpecialOfferCell.hNSMutableArray的定製電池實現問題
#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;
}
那麼,我要在這裏實現的是,當細胞已加載的圖片,它不會再次加載。
嗨,你可以請粘貼你的表視圖數據源實現代碼嗎? –
嗨@NagaMalleshMaddali我已經更新了代碼。謝謝。 – xeravim