uicollectionview中的多个选择在ios中不起作用

时间:2014-03-04 09:30:32

标签: uicollectionview uicollectionviewcell ios6.1

我在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];
 }

1 个答案:

答案 0 :(得分: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]];
}