[![as per screenshot need index 0 index 4 should display x buttons,and index 5 index 8 need +buttons.whenever tapping on buttons should change states well.][1]][1] [1]: http://i.stack.imgur.com/ytvzn.png here code. viewcontroller.m @property (nonatomic, strong) uicollectionview *collectionview; @property (nonatomic, strong) uibutton *deletebtn; @property (nonatomic, strong) nsmutablearray *photosarray; @property (nonatomic, strong) nsmutablearray *selectedphotosarray; - (void)viewdidload { _photosarray = [[nsmutablearray alloc]initwithobjects:@"angry_birds_cake.jpg", @"full_breakfast.jpg", @"green_tea.jpg", @"hamburger.jpg", @"mushroom_risotto.jpg", @"", @"", @"", @"", nil]; selectedphotosarray = [[nsmutablearray alloc]init]; nslog(@"%@",[[nsuserdefaults standarduserdefaults]objectforkey:@"table"]); if ([[nsuserdefaults standarduserdefaults] objectforkey:@"cell"]!= nil) { selectedphotosarray = [[[nsuserdefaults standarduserdefaults] arrayforkey:@"cell"] mutablecopy]; } else { selectedphotosarray = [[nsmutablearray alloc]initwithobjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",nil]; } } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { static nsstring *cellid = @"cellid"; racollectionviewcell *cell = [self.collectionview dequeuereusablecellwithreuseidentifier:cellid forindexpath:indexpath]; [cell.imageview removefromsuperview]; cell.imageview.frame = cell.bounds; nslog(@"_photosarray.count--------%ld",(unsigned long)_photosarray.count); nslog(@"indexpath.row--------%ld",(long)indexpath.row); cell.imageview.image = [uiimage imagenamed:[_photosarray objectatindex:indexpath.item]]; cell.backgroundcolor = [uicolor lightgraycolor]; [cell.contentview addsubview:cell.imageview]; deletebtn = [uibutton buttonwithtype:uibuttontypecustom]; deletebtn.frame=cgrectmake(80, 80, 19, 19); [deletebtn setimage:[uiimage imagenamed:@"ic_dele_photo.png"] forstate:uicontrolstatenormal]; deletebtn.tag = indexpath.row; [deletebtn addtarget:self action:@selector(buttonclicked:) forcontrolevents:uicontroleventtouchupinside]; [cell.contentview addsubview:deletebtn]; if (indexpath.row<5) { [deletebtn setimage:[uiimage imagenamed:@"ic_dele_photo.png"] forstate:uicontrolstatenormal]; } else { [deletebtn setimage:[uiimage imagenamed:@"ic_add_photo.png"] forstate:uicontrolstatenormal]; } if ([[selectedphotosarray objectatindex:indexpath.row] isequaltostring:@"0"]) { [deletebtn setimage:[uiimage imagenamed:@"ic_add_photo.png"] forstate:uicontrolstatenormal]; } else if([[selectedphotosarray objectatindex:indexpath.row] isequaltostring:@"1"]) { [deletebtn setimage:[uiimage imagenamed:@"ic_dele_photo.png"]forstate:uicontrolstatenormal]; } return cell; } -(void)buttonclicked :(id)sender { nslog(@"%ld",(long)[sender tag]); if ([[selectedphotosarray objectatindex:[sender tag]] isequaltostring:@"0"]) { [selectedphotosarray replaceobjectatindex:[sender tag] withobject:@"1"]; } else{ [selectedphotosarray replaceobjectatindex:[sender tag] withobject:@"0"]; } [_collectionview reloaddata]; nslog(@"selectedphotosarray == %@",selectedphotosarray); [[nsuserdefaults standarduserdefaults]setobject:selectedphotosarray forkey:@"cell"]; [[nsuserdefaults standarduserdefaults]synchronize]; }
am not able set buttons in exact cells.please can guide me.am using custom cell racollectionviewcell in declared imageview.am not able set images x button in indexpath 0 indexpath 4,and + button indexpath 5 indexpath 8.
Comments
Post a Comment