objective c - To change color only tapped image color of UICollectionView in XCode -


i've created image in nsarray follow in viewdidload.

menuimgs = @[@"image1", @"image2", @"image3", @"image4"]; 

and apply above images in uicollectionview follow.

- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {      uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"homecell" forindexpath:indexpath];     uiimage *iconimg = [uiimage imagenamed:menuimgs[arraycountchecker]];     uiimageview *cellimage = [[uiimageview alloc]initwithimage:[uiimage imagenamed:menuimgs[arraycountchecker]]];     cellimage.frame = cgrectmake(cellwidth/2-iconimg.size.width,cellheight/2-iconimg.size.height, iconimg.size.width, iconimg.size.height);     [cell addsubview:cellimage];      return cell; } 

what want want change image color when tapped on specific uicollectionviewcell in function.

- (void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath 

if want change background color of selected cell use cell.selectedbackgroundview otherwise,

intialize 1 array , store selected index in array. , when collectionview load data check index array , change image color according. when select new cell remove old index array , store new , and reload colloectionview. here code example.

   - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath     { collectionviewcustomcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"cell" forindexpath:indexpath];  cell.lblfilter.text   = filtertitles[indexpath.row]; cell.imgvfilter.image = filterimages[indexpath.row];  if([indexpath isequal:selectedindexpath]) {     cell.imgvfilter.superview.layer.bordercolor = [uicolor lightblue].cgcolor;      cell.lblfilter.textcolor = [uicolor lightblue]; } else {     cell.lblfilter.textcolor = [uicolor textgray];      cell.imgvfilter.superview.layer.bordercolor = [uicolor bordergray].cgcolor; }  return cell; }    - (void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath { [collvfilter scrolltoitematindexpath:indexpath atscrollposition:uicollectionviewscrollpositionnone animated:yes];  if (![indexpath isequal:selectedindexpath]) {     nsindexpath *indexpathtoreload = selectedindexpath;      selectedindexpath = indexpath;      [collvfilter reloaditemsatindexpaths:@[indexpathtoreload, indexpath]]; }  } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -