ios - NSMutableArray in Custom cell implementation issue -
so had code on 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
and implementation file
#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"]; (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
so when called custom cell in uitableviewcontroller array image using array has been used before, when initizialize new cell, array not refreshed. why that? because dequeuereusablecellwithidentifier?
data source:
- (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; }
so going achieve here is, when cell have loaded images, wont load again.
in specialoffercell need implement prepareforreuse
here can reset data such image array
Comments
Post a Comment