UICollectionview允许MultipleSelection不起作用的Xcode [英] UICollectionview allows multipleSelection not working xcode
本文介绍了UICollectionview允许MultipleSelection不起作用的Xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用允许多重选择来选择 Collection View
单元格图像不起作用.请提出我的想法或链接
How to select the Collection View
cell images using allows multiple selection is not working. Please suggest me idea or links
- (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).
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).
#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];
}
}
希望获得帮助.
这篇关于UICollectionview允许MultipleSelection不起作用的Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文