ios - Cell"s are not showing properly when scrolled -
i have images within collection view can select. when select image(s) wrong image cells selected. once scroll down out of view of cell cell no longer selected. how can fix issue?
the imageview defined in storyboard. assets in photo library.
this photocell.h file.
#import <uikit/uikit.h> #import <assetslibrary/assetslibrary.h> @interface photocell : uicollectionviewcell @property(nonatomic,strong) alasset * asset; @property (nonatomic,weak) iboutlet uiimageview * photoimageview;
this photocell.m file.
#import "photocell.h" @interface photocell () @end @implementation photocell #pragma mark - user made method - (void) setasset:(alasset *)asset { // 2 _asset = asset; self.photoimageview.image = [uiimage imagewithcgimage:[asset thumbnail]]; } #pragma mark - collectionview cell method -(void)prepareforreuse { } -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ photocell *cell =(photocell *)[collectionview dequeuereusablecellwithreuseidentifier:@"photocell" forindexpath:indexpath]; alasset * asset = self.assets[indexpath.row]; cell.asset = asset; cell.backgroundcolor = [uicolor redcolor]; } #pragma mark - collection view delegate -(void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath{ //nslog(@"%@ - %d", nsstringfromselector(_cmd), indexpath.item); photocell *cell = (photocell *)[collectionview cellforitematindexpath:indexpath]; chkboxbtn = [uibutton buttonwithtype:uibuttontypecustom]; [chkboxbtn setframe:cgrectmake(60, 60, 30, 30)]; [chkboxbtn settag:100]; [chkboxbtn setimage:[uiimage imagenamed:@"success.png"] forstate:uicontrolstatenormal]; [cell.contentview addsubview:chkboxbtn ]; } -(void)collectionview:(uicollectionview *)collectionview diddeselectitematindexpath:(nsindexpath *)indexpath{ // removes check box button cell after click again photocell *cell =(photocell *)[collectionview cellforitematindexpath:indexpath]; [[cell.contentview viewwithtag:100] removefromsuperview]; }
you can't add checkbox in didselectitematindexpath
, remove in diddeselectitematindexpath
, because cells reused while scrolling.
add checkbox in photocell
, , in cellforitematindexpath
function, this:
if cell.selected { checkbox.hidden = false } else { checkbox.hidden = true }
Comments
Post a Comment