iOS UITableView的多選

來源:互聯網
上載者:User

標籤:original   enum   接下來   點擊效果   content   oid   you   anim   follow   

一些列表經常需要編輯多選的功能,而UITableview內建多選刪除的功能,使用起來方便,不需要自己去做資料存放區和選中狀態轉換,可以減少不少開發時間。下面就來介紹下UITableView多選的使用。
效果 :

 UITableViewCellEditingStyle

編輯狀態UITableViewCellEditingStyle有三種模式:

  • UITableViewCellEditingStyleDelete
  • UITableViewCellEditingStyleInsert
  • UITableViewCellEditingStyleNone

多選框的風格, 只需要風格同時包含UITableViewCellEditingStyleDelete和UITableViewCellEditingStyleInsert就可以了

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;}

然後設定UITableview的Editing狀態,就可開啟tableview的多選功能。

使用內建的編輯狀態去做多選功能的好處
  • 不用去記錄資料來源中選中的數組,通過indexPathsForSelectedRows方法就可以擷取到選中的cell,做刪除操作時也會方便很多.
  • 進入退出編輯狀態方便,還有內建的動畫。ps:自訂cell時,子視圖應載入在cell的contentView上。
  • 在要求不高的情況下,內建的表徵圖就能滿足需求。
  接下來先說說全選和全不選的操作,以下都是在單section情況下
  • 做全選也簡單,遍曆一遍資料來源 然後選中每一條。
[self.listData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];        }];
  • 全不選,reload就可以變為全不選狀態了,如果不想reload這樣簡單粗暴的,也可以取出當前選中的indexpath數組,遍曆反選也可以。
        [self.tableView reloadData];        /** 遍曆反選        [[self.tableView indexPathsForSelectedRows] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {            [self.tableView deselectRowAtIndexPath:obj animated:NO];        }];         */
  • 多section情況類似,在刪除操作的時候注意點,刪除到section的cell到最後一個時 需要刪除整個section。
編輯操作,下面以刪除為例
NSMutableIndexSet *insets = [[NSMutableIndexSet alloc] init];        [[self.tableView indexPathsForSelectedRows] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {            [insets addIndex:obj.row];        }];        [self.listData removeObjectsAtIndexes:insets];        [self.tableView deleteRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationFade];                /** 資料清空情況下取消編輯狀態*/        if (self.listData.count == 0) {            self.navigationItem.rightBarButtonItem.title = @"編輯";            [self.tableView setEditing:NO animated:YES];            [self showEitingView:NO];            /** 帶MJ重新整理控制項重設狀態            [self.tableView.footer resetNoMoreData];            [self.tableView reloadData];             */        }
Cell點擊效果的問題,有的人會覺得那個藍色的背景很難看,想去掉,可以在自訂的cell裡面:
self.multipleSelectionBackgroundView = [UIView new];

還可以修改有點擊選中表徵圖的顏色,其他預設圖片的顏色也會因此而修改,

self.tintColor = [UIColor redColor];

效果:


  

如果不想使用預設表徵圖的話,也可以在自訂:

-(void)layoutSubviews{    for (UIControl *control in self.subviews){        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){            for (UIView *v in control.subviews)            {                if ([v isKindOfClass: [UIImageView class]]) {                    UIImageView *img=(UIImageView *)v;                    if (self.selected) {                        img.image=[UIImage imageNamed:@"xuanzhong_icon"];                    }else                    {                        img.image=[UIImage imageNamed:@"weixuanzhong_icon"];                    }                }            }        }    }    [super layoutSubviews];}//適配第一次圖片為空白的情況- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    [super setEditing:editing animated:animated];    for (UIControl *control in self.subviews){        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){            for (UIView *v in control.subviews)            {                if ([v isKindOfClass: [UIImageView class]]) {                    UIImageView *img=(UIImageView *)v;                    if (!self.selected) {                        img.image=[UIImage imageNamed:@"weixuanzhong_icon"];                    }                }            }        }    }    }

效果:


  

demo地址:https://github.com/yxsufaniOS/TableViewMutableSelectDemo


連結:https://www.jianshu.com/p/a6e4cb42dd03

iOS UITableView的多選

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.