2014-03-04 3 views
1

격자보기와 같은 uicollectionview에서 다중 이미지가 있습니다. 한 번에 여러 이미지를 선택하고 싶지만이 코드에 대한 아이디어를주는 코드는 없습니다.uicollectionview에서 다중 선택이 작동하지 않습니다.

나는 이미이 코드를 시도했지만 작동하지 않습니다.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath 
{ 
NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath]; 
if (self.selectedItemIndexPath) 
{ 
    // if we had a previously selected cell 
    if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame) 
    { 
     // if it's the same as the one we just tapped on, then we're unselecting it 

     self.selectedItemIndexPath = nil; 
    } 
    else 
    { 
     // if it's different, then add that old one to our list of cells to reload, and 
     // save the currently selected indexPath 

     [indexPaths addObject:self.selectedItemIndexPath]; 
     self.selectedItemIndexPath = indexPath; 
    } 
} 
else 
{ 
    // else, we didn't have previously selected cell, so we only need to save this indexPath for future reference 
    self.selectedItemIndexPath = indexPath; 

} 

// and now only reload only the cells that need updating 

[self.collectionView reloadItemsAtIndexPaths:indexPaths]; 
} 
+0

단 하나의 선택이 다중 선택에서 작동하지 않는 근무이 코드. – user3331020

+0

누구든지이 질문에 대한 해결책을주십시오. – user3331020

+0

나는이 질문에 대한 해결책을 누군가에게 주길 바랍니다. – user3331020

답변

0
// I Have Drag the UICollectionView Controller in storyboard 
static NSString * const reuseIdentifier = @"Cell"; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 

    arrImage = [[NSMutableArray alloc]initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",@"5.jpeg",@"6.jpeg",@"7.jpeg",@"8.jpeg",@"9.jpeg",@"10.jpeg",@"flower.jpeg",@"flower1.jpeg", nil]; 
    [self.collectionView setAllowsMultipleSelection:YES]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark <UICollectionViewDataSource> 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return [arrImage count]; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100]; 
    recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]; 
    [self.view addSubview:recipeImageView]; 
    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]]; 

    return cell; 
} 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath]; 
    cell.contentView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.15]; 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath]; 
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100]; 
    recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]; 
} 
관련 문제