iOS单选和多选的优雅实现

作者: coenen | 来源:发表于2019-07-17 17:51 被阅读0次

选择器 (collectionView 单选、多选)(tableView 单选、多选)

在项目工程中用的最多的就是列表了,而且在列表中总会有各种各样的处理。今天和大家分享一下单选和多选的优雅实现。

在tableview单选中:

关于选择有两个基本的方法。

第一种实现

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

其中第一个是点击选择的方法,第二个是取消的方法。
在单选的实现过程中我们可以配置model的选中结果来实现

- (void)configCellStatus:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath Status:(BOOL)status{
CustomTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
SelectModel *model = cell.selectModel;
model.isSelected = status;
cell.selectModel = model;
}

分别在两个选择方法中调用实现。在didSelectRowAtIndexPath中传入状态为YES,在didDeselectRowAtIndexPath方法中传入状态为NO。然后获取到该cell的model,从而改变状态。

此种实现仅仅满足在小量数据一次性展示的情况下,也是这种实现的缺点。原因是因为再次加载数据后,cell的加载状态改变,方法的实现仅限于加载展示的cell。

第二种实现

NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < self.dataList.count; i++) {
    SelectModel *model = self.dataList[i];
    if (i == indexPath.row) {
        model.isSelected = YES;
    }else{
        model.isSelected = NO;
    }
    [array addObject:model];
}
//更换数据源
self.dataList = array;
[tableView reloadData];

此种单选方法支持刷新数据,会遍历所有数据。

第三种实现

[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:(UITableViewScrollPositionTop)];

此种单选方法在加载数据的时候会刷新原有cell选中状态。每次刷新后全部为未选中状态。
在cell中实现

- (void)setSelected:(BOOL)selected animated:(BOOL)animated方法

这是和前两种的区别之处,前两种是在model中设置选中状态,这是直接设置

在tableview多选中:

CustomTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
SelectModel *model = cell.selectModel;
model.isSelected = !model.isSelected;
cell.selectModel = model;

多选总是比较容易的,管好当前的cell即可。让状态反转,这样能取消选中。如果不需要取消选中则每次点击都设置为YES即可。model.isSelected = YES;

在collectionView单选中:

关于选择也有两个基本的方法。原理和tableview相同,(不太清楚的可以看下上面的)

第一种实现

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

实现方法和tableview也一样,稍稍更改一下即可

- (void)configCellStatus:(UICollectionView *)collectionView IndexPath:(NSIndexPath *)indexPath Status:(BOOL)status{
SelectCollectionViewCell *cell = (SelectCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
SelectModel *model = cell.model;
model.isSelected = status;
cell.model = model;

}

第二种实现

    NSMutableArray *array = [NSMutableArray array];

    for (int i = 0; i < self.dataList.count; i++) {
        SelectModel *model = self.dataList[i];
        if (i == indexPath.row) {
            model.isSelected = YES;
        }else{
            model.isSelected = NO;
        }
        [array addObject:model];
    }
    //更换数据源
    self.dataList = array;
    [collectionView reloadData];

原理同上。

第三种实现

[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:(UICollectionViewScrollPositionTop)]

原理同上。

在collectionView多选中:

SelectCollectionViewCell *cell = (SelectCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
SelectModel *model = cell.model;
model.isSelected = !model.isSelected;
cell.model = model;

总结

在单选点击cell处理中,如果采用第一种方法,则不需要reloadData。因为reloadData会改变cell的状态,导致取消选中无效。有很多的时候就是因为reloadData 导致didDeselectItemAtIndexPath不会调用。如果采用遍历数据的情况则不需要实现该方法。

Demo走一波,有需要的可以看一下,欢迎各位大大指点与批评。

相关文章

  • iOS单选和多选的优雅实现

    选择器 (collectionView 单选、多选)(tableView 单选、多选) 在项目工程中用的最多的就是...

  • iOS tableView实现单选和多选

    今天在项目中遇到了tableView的单选需求,现在总结一下,用一个简单的demo实现了简单的单选和多选两个功能....

  • iOS tableView实现单选和多选

    今天在项目中遇到了tableView的单选需求,现在总结一下,用一个简单的demo实现了简单的单选和多选两个功能....

  • #iOS单选和多选

    开始做单选和多选的时候在网上找了好久,发现要么是单选,要么是多选,共存的教程几乎没有。因此自己研究了下,写了个简单...

  • 单选以及多选的实现 iOS

    话不多说,首先看效果图 1、定义UIView的扩展类 UIView+DBExtension.h import

  • 单选以及多选的实现 iOS

    话不多说,首先看效果图 请点击此处输入图片描述 单选.gif 请点击此处输入图片描述 多选.gif 1、定义UIV...

  • Ant Design Vue table 点击行 勾选check

    现在的需求实现单选和多选功能表格中实现单选多选功能官方demo中没有找到,特此记录下上代码 就是利用表格点击行的方...

  • 实现单选和多选删除

    项目:http://m.123.local.qushiyun.com/manager/index.php?app=...

  • iOS UITableView 单选和多选

    hello,iOS小伙伴们,最近都在搞单选和多选,我为大家总结了一下,虽然网上都能早到,但是都是零零闪闪的;几行代...

  • iOS UICollectionView单选和多选

    hello,iOS小伙伴们,上次研究了tableView,这次是UICollectionView,其实和table...

网友评论

    本文标题:iOS单选和多选的优雅实现

    本文链接:https://www.haomeiwen.com/subject/bysclctx.html