ios - Change the image of a specific cell inside a UICollectionView -
i have 5x5 grid of cells in uicollectionview, each image set this:
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { collectionviewcell *cell = (collectionviewcell *)[collectionview dequeuereusablecellwithreuseidentifier:@"circlecell" forindexpath:indexpath]; imagearray = [[nsmutablearray alloc]init]; (int i=1; i<=12; i++) { uiimage *imagetoadd = [uiimage imagenamed:[nsstring stringwithformat:@"%i", i]]; [imagearray addobject:imagetoadd]; } self.cellimageview = [[uiimageview alloc] initwithframe:[self getcirclesize]]; int = arc4random() % 12; [self.cellimageview setimage:[imagearray objectatindex:i]]; [cell addsubview:self.cellimageview]; cell.tag = i; homecoming cell; }
each image circle of random color - 1.png 12.png. detecting taps on specific cell, , getting it's color , position in collection view:
uicollectionviewcell *cell = [collectionview cellforitematindexpath:indexpath]; nsindexpath *indexpath = [collectionview indexpathforcell:cell]; self.firstcirclesection = indexpath.section; self.firstcirclerow = indexpath.row; self.firstcircleindex = (indexpath.section * 5) + (indexpath.row + 1); self.firstcirclenum = (int)cell.tag; //i have dictionary colors , keys, e.g 1 yellow, 2 blue, 3 red. line gets color: nsstring *dotcolor = [nsstring stringwithformat:@"colour: %@", [self.colordict objectforkey:[nsstring stringwithformat:@"%ld", self.firstcirclenum]]];
then sec circle tap of circle, calculating new color of each circle (a color in middle of 2 selected colors):
uicollectionviewcell *cell = [collectionview cellforitematindexpath:indexpath]; nsindexpath *indexpath = [collectionview indexpathforcell:cell]; self.secondcirclesection = indexpath.section; self.secondcirclerow = indexpath.row; self.secondcircleindex = (indexpath.section * 5) + (indexpath.row + 1); self.secondcirclenum = (int)cell.tag; nsstring *circlecolor = [nsstring stringwithformat:@"colour: %@", [self.colordict objectforkey:[nsstring stringwithformat:@"%ld", self.secondcirclenum]]]; nsinteger = (self.firstcirclenum + self.secondcirclenum)/2; nsstring *dotcolor = [nsstring stringwithformat:@"colour: %@", [self.colordict objectforkey:[nsstring stringwithformat:@"%ld", i]]]; // labels on view showing colors mix colorlabel2.text = circlecolor; positionlabel2.text = [nsstring stringwithformat: @"position: %ld", self.secondcircleindex]; mixedcolorlabel.text = [nsstring stringwithformat:@"new %@", dotcolor]; nsindexpath *indexpath2 = [nsindexpath indexpathforrow:self.firstcirclerow insection:self.firstcirclesection]; nsinteger thefirstcircleindex = (indexpath2.section * 5) + (indexpath2.row + 1); nsindexpath *indexpath3 = [nsindexpath indexpathforrow:self.secondcirclerow insection:self.secondcirclesection]; nsinteger thesecondcircleindex = (indexpath3.section * 5) + (indexpath3.row + 1); // reference 2 cells uicollectionviewcell *firstcircle = [collectionview dequeuereusablecellwithreuseidentifier:@"circlecell" forindexpath:indexpath2]; uicollectionviewcell *secondcircle = [collectionview dequeuereusablecellwithreuseidentifier:@"circlecell" forindexpath:indexpath3];
my question is, 1 time have done this, how can alter image of 2 cells - firstcircle , secondcircle? have property on custom collectionviewcell, cellimage, unable access with: firstcircle.cellimage. want alter image on both cells.
in cellforitematindexpath:
method, creating new uiimageview
, setting image array, , adding subview of cell. different cellimage
property of cell. prepare this:
either: if have configured custom collectionviewcell
in storyboard (i.e. added image view prototype cell, , hooked collectionviewcell's cellimage
outlet), needn't add together uiimageview
can use:
cell.cellimage.image = [imagearray objectatindex:i];
or: if it's not configured in storyboard (or collectionviewcell initialiser), maintain existing code, set cell.cellimage point imageview created:
cell.cellimage = self.cellimageview;
next: in other code (which assume in didselectitematindexpath
?), next line tells compiler cells of base of operations uicollectionviewcell
class:
uicollectionviewcell *cell = [collectionview cellforitematindexpath:indexpath];
but want compiler know these cells custom collectionviewcell class. amend line to:
collectionviewcell *cell = (collectionviewcell *)[collectionview cellforitematindexpath:indexpath];
(and elsewhere). done, compiler should allow utilize cellimage
property without problem.
now, reference cells indexpath2
, indexpath3
, don't utilize dequeuereusablecellwithidentifier:
method, utilize cellforitematindexpath:
(just in first line):
collectionviewcell *firstcircle = [collectionview cellforitematindexpath:indexpath2]; collectionviewcell *secondcircle = [collectionview cellforitematindexpath:indexpath3];
you can manipulate images firstcircle.cellimage.image
etc.
also, there redundancy in code: these lines:
uicollectionviewcell *cell = [collectionview cellforitematindexpath:indexpath]; nsindexpath *indexpath = [collectionview indexpathforcell:cell];
you getting cell given indexpath, , getting indexpath cell - same. can delete sec of these lines. also, in code handles secondcircle
, indexpath3
same indexpath
, , secondcircle
cell
set @ start of code. use:
collectionviewcell *secondcircle = cell;
finally, cellforitematindexpath
loads imagearray every time called - unnecessary. utilize different method load these images - perhaps viewdidload
method.
ios objective-c xcode uicollectionview
No comments:
Post a Comment