• iOS中的界面多选功能--(UICollectionView)


    文/Jacob_Pan(简书作者)
    原文链接:http://www.jianshu.com/p/9d28ebd0f5a2
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

    最近做项目接触了一些需要实现多选的功能,但总结起来方法类似;实现此功能的方法有很多,接下来介绍一种方法,利用UICollectionView实现。


    我们都知道,UICollectionView可以被认为更高级的UITableView,因此UITableView里面可以实现的在UICollectionView都可以实现,尤其针对类似瀑布流那样的界面,UICollectionView功能更强大,更方便。

    本文没有介绍UICollectionView的Cell定制,代理的设置,数据模型,userView的封装等,如有兴趣可参照下面我做的简易Demo。


    对于多选功能,显然我们会用到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。


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

    }

    }];


    按钮标题也要随之改变,因此综上所述按钮的实现方法为:

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

    }];}}

    在此基本功能已经实现,但详细具体细节本文没有给出,只是给出一种思路;如果现在你的感觉是:


    不要着急:请看Demo

    百度云升级

  • 相关阅读:
    2016"百度之星"
    codeforces 55 div2 C.Title 模拟
    codeforces 98 div2 C.History 水题
    codeforces 97 div2 C.Replacement 水题
    codeforces 200 div2 C. Rational Resistance 思路题
    bzoj 2226 LCMSum 欧拉函数
    hdu 1163 九余数定理
    51nod 1225 余数的和 数学
    bzoj 2818 gcd 线性欧拉函数
    Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5231484.html
Copyright © 2020-2023  润新知