tableview某一个section实现多选

viewcontroller.m


#import "ViewController.h"

#import "SectionOneCell.h"
#import "SectionTwoCell.h"

@interface ViewController () 

@property (nonatomic, assign) NSInteger currentEditingStyle;
@property (nonatomic, copy) NSMutableArray *selectedIndexPaths;

@end

@implementation ViewController

- (NSMutableArray *)selectedIndexPaths
{
    if (!_selectedIndexPaths)
    {
        _selectedIndexPaths = [[NSMutableArray alloc] init];
    }
    return _selectedIndexPaths;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerNib:[UINib nibWithNibName:@"SectionOneCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SectionOneCell"];
    [tableView registerNib:[UINib nibWithNibName:@"SectionTwoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SectionTwoCell"];
    [self.view addSubview:tableView];
    
    // 设置tableview可编辑
    tableView.editing = YES;
    // 设置可多选
    tableView.allowsMultipleSelectionDuringEditing = true;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
/**
 *  必须实现此代理方法
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleDelete & UITableViewCellEditingStyleInsert;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        SectionOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionOneCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section == 1)
    {
        SectionTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionTwoCell"];
        // 改变选择按钮的颜色
        cell.tintColor = [UIColor orangeColor];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        // 选中某一行
        if (self.currentEditingStyle != (UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert))
        {
            [self.selectedIndexPaths addObject:indexPath];
            
            NSLog(@"%@",self.selectedIndexPaths);
        }
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        // 取消选中某一行
        if (self.currentEditingStyle != (UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert))
        {
            [self.selectedIndexPaths removeObject:indexPath];
            NSLog(@"%@",self.selectedIndexPaths);
        }
    }
}

@end

SectionOneCell.m


#import "SectionOneCell.h"

@implementation SectionOneCell

- (void)awakeFromNib
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

/**
 *  不重写此方法,cell左边会默认有一个选的圆圈
 */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    
}

@end

SectionTwoCell.m

#import "SectionTwoCell.h"

@implementation SectionTwoCell

- (void)awakeFromNib
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end

效果图:

tableview某一个section实现多选_第1张图片

你可能感兴趣的:(iOS)