VyTcdc VyTcdc - 3 months ago 27
iOS Question

I want to add ELCImagePickerController and make remains the selected images

I am using ELCImagePickerController library because I want to select Multiple Image from gallery, and if user select few images and come out from the gallery and if they again go to the gallery, the selected image should show in that collection view, could some one help me how to do this.

Answer

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableViewSelectedPreviousCheckUnCheck;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
  NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark,*arrFetchedresult;
  NSArray *arrayFetchFromDefaults;
}

@end

@implementation ViewController

@synthesize tableViewSelectedPreviousCheckUnCheck;

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view from its nib.
  arrProductSelection = [[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iWatch",@"iMac",nil];
}
- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  arrayFetchFromDefaults = [userDefaults objectForKey:@"selectedcheckmark"];
  arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults];
  if(arrProductSelectDeSelectCheckMark.count == 0)
  {
      arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init];
     for(int j=0;j<[arrProductSelection count];j++)
     {
        [arrProductSelectDeSelectCheckMark addObject:@"deselected"];
     }
   }
   [tableViewSelectedPreviousCheckUnCheck reloadData];
}

#pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return arrProductSelection.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *strCell = @"cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
   if(cell==nil)
   {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
   }
   if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
     cell.accessoryType = UITableViewCellAccessoryNone;
   else
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
   cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row];
   return cell;
}

#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   @try
   {
      CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate];
      NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint];
      NSLog(@"%@",arrProductSelectDeSelectCheckMark);
      if([arrProductSelectDeSelectCheckMark count]==0)
      {
        for(int i=0; i<[arrProductSelection count]; i++)
        {
            [arrProductSelectDeSelectCheckMark addObject:@"deselected"];
        }
       }
       if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"])
       {
          cell.accessoryType = UITableViewCellAccessoryCheckmark;
          [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"];
       }
       else
       {
          cell.accessoryType = UITableViewCellAccessoryNone;
          [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"];
       }

       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
       [defaults setObject:arrProductSelectDeSelectCheckMark forKey:@"selectedcheckmark"];
       [defaults synchronize];
    }
    @catch (NSException *exception)
    {
       NSLog(@"The exception is-%@",exception);
    }
}
@end