ios - Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling? -
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; tvcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; if (cell == nil) cell = [[tvcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; cell.txtlabel.text = [[[arraydata objectatindex:indexpath.row] valueforkey:@"description"]stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; cell.imglabel.contentmode = uiviewcontentmodescaleaspectfill; cell.imglabel.image = nil; nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@"%@",[enclosureurlarray objectatindex:indexpath.row]]]; nsurlsessiontask *task = [[nsurlsession sharedsession] datataskwithurl:url completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) { if (data) { uiimage *image = [uiimage imagewithdata:data]; if (image) { dispatch_async(dispatch_get_main_queue(), ^{ cell.imglabel.image = image; [cell.imglabel setimage:image]; [cell.imglabel.layer setmaskstobounds:yes]; [cell.imglabel.layer setcornerradius:2.5f]; [cell setneedslayout]; }); } } }]; [task resume];
your problem have no guarantee nsurlrequests
terminate in same order started. bad because cells re-used better performance , can end strange behavior.
you can find fix here: asynchronous downloading of images uitableview gcd
or can use tools listed here address issue: https://stackoverflow.com/a/32601838/3769338
Comments
Post a Comment