UICollectionview允许multipleSelection不工作xcode



如何使用允许多选来选择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];
     }

要在集合视图中选择单个或多个项目,需要实现UIColectionViewDelegate。您还需要添加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];
        }

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

-(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]; 
    }
}

希望得到帮助。

最新更新