如何使用NSUserdefaults在按钮点击中更改UICollectionviewCell中的图像?

时间:2016-01-29 09:46:53

标签: uibutton uicollectionview nsmutablearray nsuserdefaults uicollectionviewcell

[![As per screenshot i need initially from index 0 to index 4 should display x buttons,and from index 5 to index 8 need +buttons.whenever am tapping on the buttons it should change their states as well.][1]][1]


  [1]: http://i.stack.imgur.com/YTVzN.png

Here is my 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];

}

我无法在精确的单元格中设置按钮。请任何人指导我。我使用自定义单元格RACollectionViewCell,我声明了imageview.Am无法将indexpath 0中的图像x按钮设置为indexpath 4,而+ indexpath 5到indexpath 8.

0 个答案:

没有答案