ios - UIImage in UITableView displaying the other cell's image for a second -
i trying load image asynchronously in uiimage resides in table view cell. shows image cell second before displays correct one.
this code using
[[tmcache sharedcache] objectforkey:post[@"gif"] block:^(tmcache *cache, nsstring *key, id object) { if (object) { if ([self isrowzerovisible:indexpath.section]) { [gifloader loadgifdata:object to:postgif for:feedtableview]; } return; } dispatch_async(dispatch_get_main_queue(), ^{ __block nsurl* url = [nsurl urlwithstring:post[@"gif"]]; nsurlrequest* req = [nsurlrequest requestwithurl:url]; ohurlloader* loader = [ohurlloader urlloaderwithrequest:req]; [loader startrequestwithresponsehandler:nil progress:nil completion:^(nsdata* receiveddata, nsinteger httpstatuscode) { if ([self isrowzerovisible:indexpath.section]) { [gifloader loadgifdata:receiveddata to:postgif for:feedtableview]; } [[tmcache sharedcache] setobject:receiveddata forkey:post[@"gif"]]; } errorhandler:nil]; }); }]; [gifloader loadgifdata:receiveddata to:postgif for:feedtableview]; method created loads gifs in uiimageview. in background thread , assigns image in main thread though.
are there practices need follow async loading?
i not sure wrong code. highly appreciated
edit
//loading stuff tableview - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { @autoreleasepool { //type of cells static nsstring *simpletableimageidentifier = @"gifcell"; static nsstring *simpletablebodyidentifier = @"bodycell"; static nsstring *simpletableactionsidentifier = @"actionscell"; uitableviewcell *cell = nil; //data goes in cell nsdictionary *post = [dataarray objectatindex:indexpath.section]; //user info uiimageview *useravatar; uilabel *postuserfullnamelabel; uilabel *postusername; //gif date ohattributedlabel* gifdate = nil; nsmutableattributedstring* gifdatestring; //feed gif nsstring *gifbody = [[nsstring alloc]init]; ohattributedlabel* attrlabel = nil; nsmutableattributedstring* mas; //location ohattributedlabel* giflocation = nil; nsmutableattributedstring* giflocationstring; uilabel *locationbg; //buttons uibutton *btn1 =[[uibutton alloc] initwithframe:cgrectmake(11,0,50,30)]; uibutton *btn2 =[[uibutton alloc] initwithframe:cgrectmake(69,0,50,30)]; uibutton *btn3 =[[uibutton alloc] initwithframe:cgrectmake(259,0,50,30)]; if (indexpath.row == 0) { cell = [tableview dequeuereusablecellwithidentifier:simpletableimageidentifier]; if (cell == nil){ cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableimageidentifier]; } __block uiimageview *postgif= (uiimageview *)[cell viewwithtag:104]; postgif.image = nil; [[tmcache sharedcache] objectforkey:post[@"gif"] block:^(tmcache *cache, nsstring *key, id object) { if (object) { if ([self isrowzerovisible:indexpath.section]) { [gifloader loadgifdata:object to:postgif for:feedtableview]; } return; } dispatch_async(dispatch_get_main_queue(), ^{ __block nsurl* url = [nsurl urlwithstring:post[@"gif"]]; nsurlrequest* req = [nsurlrequest requestwithurl:url]; ohurlloader* loader = [ohurlloader urlloaderwithrequest:req]; [loader startrequestwithresponsehandler:nil progress:nil completion:^(nsdata* receiveddata, nsinteger httpstatuscode) { if ([self isrowzerovisible:indexpath.section]) { [gifloader loadgifdata:receiveddata to:postgif for:feedtableview]; } [[tmcache sharedcache] setobject:receiveddata forkey:post[@"gif"]]; } errorhandler:nil]; }); }]; postgif.layer.cornerradius = 2.0; postgif.layer.maskstobounds = yes; postgif.clipstobounds = yes; locationbg = (uilabel *)[cell viewwithtag:106]; useravatar = (uiimageview *)[cell viewwithtag:100]; if ([post [@"user_avatar"] length ] > 0) { nsstring *img = [@"https://d1f7i732a4e7fw.cloudfront.net/" stringbyappendingstring:post [@"user_avatar"]]; [useravatar setimagewithurl:[nsurl urlwithstring:img] placeholderimage:[uiimage imagenamed:@"userdefaultpicture.png"]]; }else{ useravatar.image = [uiimage imagenamed:@"userdefaultpicture.png"]; } useravatar.layer.cornerradius = 18.0; useravatar.layer.borderwidth = 1.0; useravatar.layer.bordercolor = (__bridge cgcolorref)([uicolor lightgraycolor]); useravatar.clipstobounds = yes; postuserfullnamelabel = (uilabel *)[cell viewwithtag:101]; postuserfullnamelabel.text = post[@"user_fullname"]; postusername = (uilabel *)[cell viewwithtag:102]; postusername.text = [nsstring stringwithformat:@"@%@",post[@"user_username"]]; gifdate = [[ohattributedlabel alloc] initwithframe:cgrectmake(246,9,60,21)]; gifdate.autoresizingmask = uiviewautoresizingnone; gifdate.centervertically = yes; gifdate.highlightedtextcolor = [uicolor whitecolor]; gifdate.tag = 103; gifdate.backgroundcolor = [uicolor clearcolor]; gifdate.extendbottomtofit = no; gifdatestring = [nsmutableattributedstring attributedstringwithstring:[nsstring stringwithformat:@"\ue003 %@",post[@"date"]]]; [gifdatestring setfont:[uifont fontwithname:@"helvetica neue" size:12.0]]; [gifdatestring setfont:[uifont fontwithname:@"icomoon" size:10.0] range:nsmakerange(0,1)]; [gifdatestring settextcolor:[uicolor whitecolor]]; [gifdatestring settextalignment:kcttextalignmentright linebreakmode:kctlinebreakbytruncatingtail]; [ohasbasicmarkupparser processmarkupinattributedstring:gifdatestring]; gifdate.attributedtext = gifdatestring; if (!(post[@"latitude"] == (id)[nsnull null])){ //location of post giflocation = [[ohattributedlabel alloc] initwithframe:cgrectmake(165,27,141,21)]; giflocation.autoresizingmask = uiviewautoresizingnone; giflocation.centervertically = yes; giflocation.highlightedtextcolor = [uicolor whitecolor]; giflocation.tag = 107; giflocation.backgroundcolor = [uicolor clearcolor]; giflocation.extendbottomtofit = no; giflocationstring = [nsmutableattributedstring attributedstringwithstring:[nsstring stringwithformat:@"\uf041 %@",post[@"locationname"]]]; [giflocationstring setfont:[uifont fontwithname:@"helvetica neue" size:12.0]]; [giflocationstring setfont:[uifont fontwithname:@"icomoon" size:12.0] range:nsmakerange(0,1)]; [giflocationstring settextcolor:[uicolor whitecolor]]; [giflocationstring settextalignment:kcttextalignmentright linebreakmode:kctlinebreakbytruncatingtail]; [ohasbasicmarkupparser processmarkupinattributedstring:giflocationstring]; giflocation.attributedtext = giflocationstring; } } else if (indexpath.row == 1) { cell = [tableview dequeuereusablecellwithidentifier:simpletablebodyidentifier]; if (cell == nil){ cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletablebodyidentifier]; } //body of gif // add more here http://www.easyapns.com/iphone-emoji-alerts gifbody = post[@"body"]; attrlabel = [[ohattributedlabel alloc] initwithframe:cgrectmake(10,5,klabelwidth,tableview.rowheight-2*klabelvmargin)]; attrlabel.autoresizingmask = uiviewautoresizingflexibleheight; attrlabel.centervertically = no; attrlabel.automaticallyaddlinksfortype = nstextcheckingalltypes; attrlabel.delegate = self; attrlabel.highlightedtextcolor = uicolorfromrgb(0x333333); attrlabel.tag = kattributedlabeltag; attrlabel.backgroundcolor = [uicolor clearcolor]; attrlabel.extendbottomtofit = yes; [cell.contentview addsubview:attrlabel]; attrlabel = (ohattributedlabel*)[cell viewwithtag:kattributedlabeltag]; mas = [nsmutableattributedstring attributedstringwithstring:gifbody]; [mas setfont:[uifont fontwithname:@"helvetica neue" size:14.0]]; [mas settextcolor:uicolorfromrgb(0x333333)]; [mas settextalignment:kcttextalignmentleft linebreakmode:kctlinebreakbywordwrapping]; [ohasbasicmarkupparser processmarkupinattributedstring:mas]; attrlabel.attributedtext = mas; } else if (indexpath.row == 2) { cell = [tableview dequeuereusablecellwithidentifier:simpletableactionsidentifier]; if (cell == nil){ cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableactionsidentifier]; } [self customizebuttons:btn1]; btn1.tag = (indexpath.section +1)*200; btn1.titlelabel.font = [uifont fontwithname:@"icomoon" size:16.0]; if ([post[@"is_favoring"] boolvalue]) { [btn1 settitlecolor:uicolorfromrgb(0xe4717a) forstate:uicontrolstatenormal]; }else{ [btn1 settitlecolor:[uicolor lightgraycolor] forstate:uicontrolstatenormal]; } [btn1 settitle:@"\uf004" forstate:uicontrolstatenormal]; btn1.contentverticalalignment = uicontrolcontentverticalalignmentcenter; [btn1 addtarget:self action:@selector(favoraction:) forcontrolevents:uicontroleventtouchupinside]; [cell.contentview addsubview:btn1]; [self customizebuttons:btn2]; btn2.tag = (indexpath.section +1)*2000; btn2.titlelabel.font = [uifont fontwithname:@"icomoon" size:16.0]; [btn2 settitlecolor:[uicolor lightgraycolor] forstate:uicontrolstatenormal]; [btn2 settitle:@"\ue000" forstate:uicontrolstatenormal]; [cell.contentview addsubview:btn2]; btn2.contentverticalalignment = uicontrolcontentverticalalignmentcenter; [btn2 addtarget:self action:@selector(commentaction:) forcontrolevents:uicontroleventtouchupinside]; [self customizebuttons:btn3]; btn3.titlelabel.font = [uifont fontwithname:@"icomoon" size:16.0]; [btn3 settitlecolor:[uicolor lightgraycolor] forstate:uicontrolstatenormal]; [btn3 settitle:@"\ue001" forstate:uicontrolstatenormal]; btn3.tag = (indexpath.section +1)*20000; [cell.contentview addsubview:btn3]; btn3.contentverticalalignment = uicontrolcontentverticalalignmentcenter; [btn3 addtarget:self action:@selector(otheraction:) forcontrolevents:uicontroleventtouchupinside]; } //paginiation if (indexpath.section == [dataarray count]-4 && indexpath.row == 1) { [self loadnextpage]; } return cell; } }
the reason seeing image cell efficiency's sake, table cells reused. around problem, set image nil
after dequeue reusable cell in tableview:cellforrowatindexpath:
.
Comments
Post a Comment