ios - Images are changing in collectionView when i scroll it -
this question has answer here:
n code below, collectionview scroll smoothly, if scroll extremely fast, there incorrect image shows , changes correct 1 scrolling decelerates. why isn't setting below code using my code :
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { uicollectionviewcell *cell; cell= [collectionview dequeuereusablecellwithreuseidentifier:@"identifier" forindexpath:indexpath]; cell.backgroundcolor = [uicolor whitecolor]; nsmutablearray *arr_assect=[menuarray objectatindex:indexpath.row]; backview=[[uiview alloc]initwithframe:cgrectmake(0, 0, cell.bounds.size.width, cell.bounds.size.height)]; uiimageview *dotlines=[[uiimageview alloc]init]; dotlines.frame=cgrectmake(1, 0, cell.bounds.size.width-1, cell.bounds.size.height); dotlines.image=[uiimage imagenamed:@"dottedline"]; [cell addsubview:dotlines]; uiimageview*cellimage=[[uiimageview alloc]initwithframe:cgrectmake(18, 10,backview.bounds.size.height-35, backview.bounds.size.height-40)]; nslog(@"arrayimage%@",[[menuarray valueforkey:@"category_image"] objectatindex:indexpath.row]); nsstring *str_homepage = [nsstring stringwithformat:@"%@",[[menuarray valueforkey:@"home_page"] objectatindex:indexpath.row]]; nslog(@"str_home_page%@",str_homepage); nsstring *imageurl= [arr_assect valueforkey:@"category_image"]; if ([[imagecache sharedimagecache] doesexist:imageurl]) { cellimage.image=[uiimage imagewithdata:[[imagecache sharedimagecache] getimage:imageurl]]; } else{ dispatch_queue_t queue=dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ nsdata *imagedata=[nsdata datawithcontentsofurl:[nsurl urlwithstring:imageurl]]; dispatch_async(dispatch_get_main_queue(), ^{ cellimage.image=[uiimage imagewithdata:imagedata]; }); [[imagecache sharedimagecache] addimage:imageurl withdata:imagedata]; }); } uilabel *lblcategorytitle =(uilabel *) [cell viewwithtag:5]; if (!lblcategorytitle) { lblcategorytitle=[[uilabel alloc]init]; [cell.contentview addsubview:lblcategorytitle]; } [lblcategorytitle setfont: [uifont fontwithname:@"helvetica" size:10]]; lblcategorytitle.tag=5; lblcategorytitle.textalignment = nstextalignmentcenter; lblcategorytitle.frame=cgrectmake(backview.frame.origin.x, cellimage.frame.origin.y+cellimage.frame.size.height+0,cell.frame.size.width , 25); lblcategorytitle.textcolor = [uicolor blackcolor]; lblcategorytitle.backgroundcolor = [uicolor clearcolor]; lblcategorytitle.numberoflines = 2; nsstring *abc = [arr_assect valueforkey:@"categorynm"]; abc = [nsstring stringwithformat:@"%@%@",[[abc substringtoindex:1] uppercasestring],[abc substringfromindex:1] ]; nslog(@"abc = %@",abc); if (app_share.language_int == 0) { lblcategorytitle.text =abc; } else if (app_share.language_int == 2) { lblcategorytitle.text =[arr_assect valueforkey:@"categorynmbl"]; } [cell addsubview:cellimage]; [cell.contentview addsubview:backview]; return cell; }
in order fix issue should prepare cell reused. there method called prepareforreuse on uicollectionviewcell , uitableviewcell. if not clean content before cell reused, might see content previous cell.
it not affect if use labels/textviews , content ready setup. however, if use background image loading (as in example) or other background tasks, affect you.
here example of implementation:
- (void)prepareforreuse { _titlelabel.text = nil; _avatarimageview.image = nil; [super prepareforreuse]; }
Comments
Post a Comment