【问题标题】:multiple selection in uicollectionview not working in iosuicollectionview中的多项选择在ios中不起作用
【发布时间】:2014-03-04 09:30:31
【问题描述】:

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

【问题讨论】:

  • 此代码仅适用于单选,不适用于多选。
  • 请任何人为这个问题提供解决方案。
  • 我已经使用谷歌尝试了很长时间,请任何人为这个问题提供解决方案......

标签: uicollectionview uicollectionviewcell ios6.1


【解决方案1】:
// 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]];
}

【讨论】:

    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2013-08-08
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多