ios - Changing the state of the CollectionView Cell -
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ //nslog(@"enter category cell"); categoryviewcell* cell = (categoryviewcell*)[collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath]; [cell.imgcat setimage:[uiimage imagenamed:[categoryimages objectatindex:indexpath.row]]]; [cell.labelcatname settext:[[nsstring stringwithformat:@"%@", [catname objectatindex:indexpath.row]] capitalizedstring]]; if([categories[[nsstring stringwithformat:@"%d",(int)indexpath.row]] isequal:@yes]) { //nslog(@"set border"); cell.layer.bordercolor = [uicolor redcolor].cgcolor; cell.layer.borderwidth = 3; } return cell; } -(void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath { categoryviewcell* cell = (categoryviewcell*)[collectionview cellforitematindexpath:indexpath]; cell.layer.borderwidth = 3; cell.layer.bordercolor = [uicolor redcolor].cgcolor; //nslog(@"%i", (int)indexpath.item); categories[[nsstring stringwithformat:@"%i", (int)indexpath.item]] = @yes; } -(void) collectionview:(uicollectionview *)collectionview diddeselectitematindexpath:(nsindexpath *)indexpath { categoryviewcell* cell = (categoryviewcell*)[collectionview cellforitematindexpath:indexpath]; cell.layer.bordercolor = [uicolor clearcolor].cgcolor; categories[[nsstring stringwithformat:@"%i", (int)indexpath.item]] = @no; }
the problem: above code show collectionview cells selected @ default. however, states of these selected cells not selected. have tap twice deselect them because first tap select them, , second tap deselect them.
i have tried set selected cell doesn't work either. cell have red border whenever user selected cell , clearcolor when user deselect cell.
i tried:
cell.selected = yes;
but permanently gives collectionview cell red border.
and add in cellforitematindexpath method still doesn't trick.
[self collectionview:collectionview didselectitematindexpath:indexpath];
categoryviewcell.m
-(void) setselected:(bool)selected animated:(bool)animated { [super setselected:selected]; //nslog(@"pass select animated"); if (selected) { self.flaghighlight = no; self.selected = no; self.layer.bordercolor = [uicolor clearcolor].cgcolor; self.layer.borderwidth = 3; } else { self.flaghighlight = yes; self.selected = yes; self.layer.bordercolor = [uicolor redcolor].cgcolor; self.layer.borderwidth = 3; } }
how pre-select cell when view loaded programmatically? or better change state of cell being selected.
thanks in advance.
ending answering own problem.
so use [collectionview selectitematindexpath:indexpath animated:no scrollposition:uicollectionviewscrollpositionnone];
in cellforitematindexpath.
serve purpose nicely.
Comments
Post a Comment