原文链接:http://www.jianshu.com/p/9d28ebd0f5a2
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
最近做项目接触了一些需要实现多选的功能,但总结起来方法类似;实现此功能的方法有很多,接下来介绍一种方法,利用UICollectionView实现。
![](http://upload-images.jianshu.io/upload_images/310248-fae8c79938b54301.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
我们都知道,UICollectionView可以被认为更高级的UITableView,因此UITableView里面可以实现的在UICollectionView都可以实现,尤其针对类似瀑布流那样的界面,UICollectionView功能更强大,更方便。
本文没有介绍UICollectionView的Cell定制,代理的设置,数据模型,userView的封装等,如有兴趣可参照下面我做的简易Demo。
![](http://upload-images.jianshu.io/upload_images/310248-c304809cb3024605.gif?imageMogr2/auto-orient/strip)
对于多选功能,显然我们会用到UICollectionView的两个方法:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode
同时用户可能会多次选择、取消操作,也就是说我们允许多次点击(multiple touch),为了更好处理这样操作,UICollectionView提供方法:
- (void)performBatchUpdates:(void (^ __nullable)(void))updates completion:(void (^ __nullable)(BOOL finished))completion; // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.
注意:在建立UICollectionView时,它的allowsMultipleSelection属性一定设置成YES。
![](http://upload-images.jianshu.io/upload_images/310248-3a645fcb4fd8a46e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在Demo中创建了一个Button,点击全选,按钮title改变,再次点击全部取消,此时需要对UICollectionView中的indexPath.item进行遍历,则创建了一个NSMutableIndexSet来增加和删除:
@property (nonatomic, strong) NSMutableIndexSet* selectedIndexSet;
当前没有选择时,我们会把它加入进去;选择后再次选择,会删除它。部分代码如下:
if ([self collectionView:self.contactsPickerView shouldSelectItemAtIndexPath:indexPath]) {
[self.contactsPickerView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self.selectedIndexSet addIndex:indexPath.item];
}
if ([self collectionView:self.contactsPickerView shouldDeselectItemAtIndexPath:indexPath]) {
[self.contactsPickerView deselectItemAtIndexPath:indexPath animated:YES];
[self.selectedIndexSet removeIndex:indexPath.item];
}
到此为止,读者也许已经想到,针对全选和全不选,只要遍历即可,下面为本人用的方法:
全选:for (NSUInteger index = 0; index < count; ++index) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldSelectItemAtIndexPath:indexPath]) {
[self.contactsPickerView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self.selectedIndexSet addIndex:indexPath.item];
}
}
全不选:[self.selectedIndexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL * _Nonnull stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldDeselectItemAtIndexPath:indexPath]) {
[self.contactsPickerView deselectItemAtIndexPath:indexPath animated:YES];
[self.selectedIndexSet removeIndex:indexPath.item];
}
}];
![](http://upload-images.jianshu.io/upload_images/310248-e3df25b3f213fc7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
按钮标题也要随之改变,因此综上所述按钮的实现方法为:
- (IBAction)handleToggleSelectionBtn:(id)sender {
NSUInteger count = [self.contacts count];
BOOL allEnabledContactsSelected = [self allEnabledContactsSelected];
if (!allEnabledContactsSelected) {
[self.contactsPickerView performBatchUpdates:^{
for (NSUInteger index = 0; index < count; ++index) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldSelectItemAtIndexPath:indexPath]) {
[self.contactsPickerView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self.selectedIndexSet addIndex:indexPath.item];
}}} completion:^(BOOL finished) {
[self updateToggleSelectionButton];
}];} else {
[self.contactsPickerView performBatchUpdates:^{
[self.selectedIndexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL * _Nonnull stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldDeselectItemAtIndexPath:indexPath]) {
[self.contactsPickerView deselectItemAtIndexPath:indexPath animated:YES];
[self.selectedIndexSet removeIndex:indexPath.item];
}}];} completion:^(BOOL finished) {
[self updateToggleSelectionButton];
}];}}
在此基本功能已经实现,但详细具体细节本文没有给出,只是给出一种思路;如果现在你的感觉是:
![](http://upload-images.jianshu.io/upload_images/310248-0bc019f20244d22f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
不要着急:请看Demo