ios - UITableView image changing while scrolling -
i have uitableview
custom cell. custom cell contains uiview
. image loaded here asynchronously. when scrolls table view, images in cell changes. here code:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"mycell"; msappointmentsmodel *data = [self.appointments objectatindex:indexpath.row]; msappointmentscell *cell = (msappointmentscell*)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { nsarray *ar = [[nsbundle mainbundle] loadnibnamed:[common checkdeviceforcontroller:@"msappointmentscell"] owner:nil options:nil]; (id eachobject in ar) { if ([eachobject iskindofclass:[uitableviewcell class]]) { cell = eachobject; } } cell.accessorytype = uitableviewcellaccessorydisclosureindicator; dispatch_async(dispatch_get_global_queue(0, 0), ^{ nsurl *imgurl = [nsurl urlwithstring:[data.cusdetails objectforkey:@"customer_image"]]; nsdata *imgdata = [nsdata datawithcontentsofurl:imgurl]; uiimage *image = [uiimage imagewithdata:imgdata]; dispatch_sync(dispatch_get_main_queue(), ^{ [cell.medallionview setimage:image]; }); }); if (data.comp_status == yes) { cell.status.image = [common imageresize:[uiimage imagenamed:@"checkmark_green.png"] andresizeto:cell.status.frame.size]; } } cell.custname.text = [data.cusdetails objectforkey:@"customer_name"]; cell.service.text = [data service]; cell.empname.text = [nsstring stringwithformat:@"by %@",[data.empdetails objectforkey:@"employee_name"]]; return cell; }
please me.
try this,
1: take array caching image nsmutablearray * imageicons;
2: after downloading data add null values array;
imageicons=[[nsmutablearray alloc] init]; (int i=0; i<[self.appointments count]; i++) { [imageicons insertobject:[nsnull null] atindex:i]; }
3:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { if(indexpath.row < [imageicons count] && [imageicons objectatindex:indexpath.row] != [nsnull null] ) { [cell.bookimage setimage:[imageicons objectatindex:indexpath.row]]; nslog(@"array hit"); } else{ nslog(@"count of %i size of %li array , row number %i",[imageicons count],sizeof(imageicons),indexpath.row); [nsthread detachnewthreadselector:@selector(downloadimageinbackground:) totarget:self withobject:indexpath]; } } -(void)downloadimageinbackground:(nsindexpath *)path { msappointmentsmodel *data = [self.appointments objectatindex:indexpath.row]; nsstring *str=[data.cusdetails objectforkey:@"customer_image"]; imagetodisplay = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:str]]]; if (imagetodisplay==nil) { // set deault image [imageicons replaceobjectatindex:path.row withobject:[uiimage imagenamed:@"defaultimg.png"]]; } else { msappointmentscell *cell=(msappointmentscell *)[self.booktableview cellforrowatindexpath:path]; [cell.bookimage setimage:imagetodisplay]; //nslog(@"count of %i size of %li array , row number %i",[imageicons count],sizeof(imageicons),path.row); [imageicons replaceobjectatindex:path.row withobject:imagetodisplay]; } }
Comments
Post a Comment