程序员

tableView和collectionView多选编辑的自定义

2017-04-19  本文已影响199人  指尖书法

一 . tableView自定义多选编辑模式

  1. 在合适的地方开启编辑模式

     [self.tableView setEdting:YES animated:YES] 
    
  2. 设置编辑样式

//返回Cell的编辑样式。
 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //现在存在一个问题,如果用这种方式,就无法实现tableView的左右滑动出编辑菜单了。
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
//这个方式,就是返回多选样式
} 
  1. 高潮来了:怎么改变系统自带的那个样式呢?

二 . collectionViewCell的多选样式

这个相对tableView来说有点难度,因为没有专门的可以设置的editing方法,只有多选模式.那么,可以从这个下手.

  1. 在合适的时机,将collectionViewCell多选模式设置为同意.
    self.collectionView.allowsMultipleSelection = YES;
  2. 高潮:改变样式
    collectionViewCell里面没有相应的selectionStyle 但是有selectBackgroudView.这个一样拉风.

整体代码可以写collectionViewCellForRow代理方法里面.

注意 : 在开启collectionView.allowsMultipleSelection的那个方法的同时,配合着collectionViewreload data 方法才是这一切改变的真正开始.至于为什么,请自行体会.
//如果是多cell编辑状态就改变背景
    if (collectionView.allowsMultipleSelection == YES) {
        
        //选择背景
        UIView *view = [[UIView alloc]initWithFrame:cell.bounds];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pin_sel"]];
        [view addSubview:imageView];
        view.backgroundColor = XHHGrey;
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(view);
            make.right.equalTo(view);
        }];
        cell.selectedBackgroundView = view;
        
        //非选择背景
        UIView *view2 = [[UIView alloc]initWithFrame:cell.bounds];
        UIImageView *imageView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pin_nor"]];
        [view2 addSubview:imageView2];
        [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(view2);
            make.right.equalTo(view2);
        }];
        cell.backgroundView =view2;

    }else {
        cell.selectedBackgroundView = nil;
        cell.backgroundView = nil;
    }

这是我今天自己研究出来的方法,如果有更好的解决办法,请给我留言,让我学习一下,小弟万分感谢.
上一篇 下一篇

猜你喜欢

热点阅读