UICollectionview 允许多个选择不起作用 xcode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20090657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:19:00  来源:igfitidea点击:

UICollectionview allows multipleSelection not working xcode

iphoneobjective-cxcodeuicollectionview

提问by user241641

How to select the Collection Viewcell images using allows multiple selection is not working. Please suggest me idea or links

如何Collection View使用允许多选来选择单元格图像不起作用。请建议我的想法或链接

- (void)viewDidLoad 
  {    
   [super viewDidLoad];
   NSLog(@"photo viewer");
   [self.collectionView setPagingEnabled:NO];
   self.collectionView.allowsSelection=YES;
   [self.collectionView registerClass:[CWPhotoGalleryCell class]
    forCellWithReuseIdentifier:CWPhotoGalleryCellIdentifier];

   [self.collectionView scrollToItemAtIndexPath:self.selectedIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];

    }
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {   
    return 1;
    }
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:
    (NSInteger)section
    {
      return [self.imageArray count];
    }

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

     CWPhotoGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CWPhotoGalleryCellIdentifier
                                                                       forIndexPath:indexPath];

     cell.navigationControllerContainer = self.navigationController;    
     ALAsset *photo             = [self.imageArray objectAtIndex:indexPath.row];
     ALAssetRepresentation *rep = [photo defaultRepresentation];     
    CGImageRef ref = [rep fullScreenImage];
    UIImage *img   = [[UIImage alloc] initWithCGImage:ref];      
     cell.image = img;
    NSLog(@"clicked");   
    return cell;
   }

    -(CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return self.view.bounds.size;
    }

   - (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
     }

回答by Greg

To select single or multiple items in collection view you need to implement UIColectionViewDelegate. You also need to add NSMutableArray where you will keep selected items (selectedItems).

要在集合视图中选择单个或多个项目,您需要实现 UICollectionViewDelegate。您还需要添加 NSMutableArray,您将在其中保留所选项目 (selectedItems)。

    #pragma mark - UICollectionViewDelegate
        - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
        {
            Item *item = self.yourArray[indexPath.row]; 
            [selectedItems addObject: item];
        }
        - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
       {
            Item *item = self.yourArray[indexPath.row]; 
            [selectedItems removeObject:item];
        }

The last thing left to do is add button to toogle between selected and non-selected state:

剩下要做的最后一件事是添加按钮以在选中和未选中状态之间切换:

-(IBAction)toogleButtonPressed:(id)sender
{
    UIBarButtonItem *toogleButton = (UIBarButtonItem *)sender; 
    if (!self.selected)
    {
        self.selected = YES;
        [toogleButton setTitle:@"Done"];
        [self.collectionView setAllowsMultipleSelection:YES];
    }
    else
    {
        self.selected = NO;
        [toogleButton setTitle:@"Select"];     
        [self.collectionView setAllowsMultipleSelection:NO];
        for(NSIndexPath *iP in self.collectionView.indexPathsForSelectedItems)
        { 
            [self.collectionView deselectItemAtIndexPath:iP animated:NO]; 
        }
        [selectedItems removeAllObjects]; 
    }
}

Hope this help.

希望这有帮助。