ios - Reusing cell causes unwanted behaviour when updating cell view -
i want ot set border selected cell, save cell property represents selected 1 , manipulate it:
-(void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath{ nsarray *eyearray = [self eyesarrayconfigure][indexpath.row]; if (indexpath.row==5) { int r = arc4random() % 6; eyearray = [self eyesarrayconfigure][r]; } [borderedcell.contentview.layer setborderwidth:0.0f] ; uicollectionviewcell *cell = [collectionview cellforitematindexpath:indexpath]; borderedcell = cell; [borderedcell.contentview.layer setbordercolor:self.view.tintcolor.cgcolor]; [borderedcell.contentview.layer setborderwidth:3.0f]; } and cellforview: (im usinng 2 types of cell identifiers because 1 cell contains label - "random cell":
-(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { if (indexpath.row ==5) { uicollectionviewcell *randomcell =[collectionview dequeuereusablecellwithreuseidentifier:@"randomcell" forindexpath:indexpath]; randomcell.backgroundcolor = [uicolor purplecolor]; borderedcell = randomcell; [borderedcell.contentview.layer setbordercolor:self.view.tintcolor.cgcolor]; [borderedcell.contentview.layer setborderwidth:3.0f]; homecoming randomcell; } uicollectionviewcell *mycell = [collectionview dequeuereusablecellwithreuseidentifier:@"cell" forindexpath:indexpath]; nsarray *eyearray = [self eyesarrayconfigure][indexpath.row]; mycell.backgroundview = [[uiimageview alloc] initwithimage:eyearray[1]]; homecoming mycell; } what if click 1 cell fine till scroll , weird behaviour when couple of cells might border.
thanks help.
the solution utilize view model. doing cells' background views. apply same concept border.
@interface mycollectionview () @property (nonatomic) nsinteger selectedrow; @end -(void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath { //update self.selectedrow , either reload entire collection view or selected , selected. self.selectedrow = indexpath.row; [collectionview reloaddata]; } -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { uicollectionviewcell *mycell = [collectionview dequeuereusablecellwithreuseidentifier:@"cell" forindexpath:indexpath]; cgfloat borderwidth = (indexpath.row == self.selectedrow) ? 3.0 : 0.0; [mycell.contentview.layer setborderwidth:borderwidth]; homecoming mycell; } ios objective-c uicollectionview uicollectionviewcell
No comments:
Post a Comment