ios - How to exclude header from reloading when using reloadData in UICollectionView -
i have uicollectionview
using csstickyheaderflowlayout
mimic header behavior in uitableview
. inside header there segmentedcontrol
control data on uicollectionview
. want reload data when tap segment (calling api) , perform reloaddata
called
- (uicollectionreusableview *)collectionview:(uicollectionview *)collectionview viewforsupplementaryelementofkind:(nsstring *)kind atindexpath:(nsindexpath *)indexpath
so best approach reload data not header, because when reloaddata
header reload too, , segment first state.
here code viewforsupplementaryelementofkind
- (uicollectionreusableview *)collectionview:(uicollectionview *)collectionview viewforsupplementaryelementofkind:(nsstring *)kind atindexpath:(nsindexpath *)indexpath { uicollectionreusableview *reusableview = nil; if (kind == uicollectionelementkindsectionheader) { segmentheaderview *collectionheader= [collectionview dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:@"header" forindexpath:indexpath]; hmsegmentedcontrol *segmentedcontrol = [[hmsegmentedcontrol alloc] initwithsectiontitles:@[@"popular", @"lelang"]]; [segmentedcontrol setframe:cgrectmake(0, 0, self.view.frame.size.width, 45)]; [segmentedcontrol addtarget:self action:@selector(segmentedcontrolchangedvalue:) forcontrolevents:uicontroleventvaluechanged]; segmentedcontrol.backgroundcolor = [nconfig flatbuttongray]; segmentedcontrol.selectionindicatorcolor = [uicolor whitecolor]; segmentedcontrol.selectionindicatorboxopacity=1; segmentedcontrol.titletextattributes = @{nsforegroundcolorattributename : [nconfig flatbuttonorange]}; segmentedcontrol.selectedtitletextattributes = @{nsforegroundcolorattributename : [nconfig flatbuttonorange]}; segmentedcontrol.selectionstyle = hmsegmentedcontrolselectionstylebox; segmentedcontrol.selectedsegmentindex = hmsegmentedcontrolnosegment; segmentedcontrol.selectionindicatorlocation = hmsegmentedcontrolselectionindicatorlocationnone; segmentedcontrol.shouldanimateuserselection = no; [segmentedcontrol setselectedsegmentindex:0 animated:yes]; [collectionheader addsubview:segmentedcontrol]; reusableview = collectionheader; } return reusableview; }
any advice? :)
your mistake store selection inside view layer can lost. should set property new selection index when user selects different segment. can react changed property reloading data. before return header view set selected segment index stored. way selection never lost.
in addition should avoid reloaddata , instead reload items changed.
Comments
Post a Comment