1

我正在使用 ELCImagePickerController 库,因为我想从图库中选择多张图片,如果用户选择了几张图片并从图库中出来,如果他们再次进入图库,则所选图片应该显示在该集合视图中,有人可以帮忙吗我该怎么做。

4

1 回答 1

2

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

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

@end

视图控制器.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
于 2016-08-08T10:22:12.013 回答