iOS UITableViewCell取消与全选状态

UITableViewCell取消与全选状态#import "ViewController.h"

@interface ViewController ()

@property  (nonatomic, assign)BOOL isAllSelected;

@property (nonatomic,  strong)NSArray  *dataArray;

@property (nonatomic,  strong)UITableView  *tableView;

@property (nonatomic, strong)NSMutableArray *selectedArrM;

@property (strong, nonatomic) NSMutableArray  *selectIndexs; //多选选中的行

//@property (nonatomic, assign)BOOL isSelected;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"多选cellDemo";

    self.selectedArrM = [NSMutableArray array];


    self.dataArray = @[@"2",@"3",@"4",@"44565",@"6",@"7",@"8",@"9",@"0",@"13",@"12",@"14",@"15",@"17",@"19",@"20"];

    //为左边的item设置标题和动作

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"全选" style:UIBarButtonItemStylePlain target:self action:@selector(selecteAllCells:)];

    [self customTableView];

}

- (void)customTableView{

    self.tableView=  [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tableView.delegate=self;

    self.tableView.dataSource=  self;

//    self.tableView.editing = YES;

//    self.tableView.allowsMultipleSelectionDuringEditing = YES;

    //rself.tableView = tableV;


    [self.view addSubview:self.tableView ];

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell  *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableViewCell"];

    if(cell ==nil) {

        cell =  [[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];

    }

    cell.accessoryType = UITableViewCellAccessoryNone;

    for(NSIndexPath*indexinself.selectIndexs) {

        if(index ==  indexPath) {

            cell.accessoryType  =UITableViewCellAccessoryCheckmark;

            break;

        }

    }

    cell.textLabel.text=self.dataArray[indexPath.row];

    returncell;

}

//获取tableView上面所有的cell

- (NSArray*)cellsForTableView:(UITableView*)tableView{

    NSIntegersections = tableView.numberOfSections;

    NSMutableArray *cells = [[NSMutableArray alloc]init];

     for(intsection =0; section < sections; section++) {

     NSIntegerrows = [tableView  numberOfRowsInSection:section];

     for(introw =0; row < rows; row++) {

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

         [cellsaddObject:[tableViewcellForRowAtIndexPath:indexPath]];

          }

     }

    returncells;

}

#pragma mark Actions

- (void)selecteAllCells:(UIBarButtonItem*)sender {

    NSArray *arr = [self.tableView indexPathsForVisibleRows];

    if (_isAllSelected == YES) {

        _isAllSelected = NO;

        [sendersetTitle:@"取消"];

        for(NSIndexPath*indexPathinarr) {

            //根据索引,获取cell 然后就可以做你想做的事情啦

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            cell.accessoryType = UITableViewCellAccessoryNone; //切换为选中


        }

        for(inti =0; i

            [self.selectedArrM  removeObject:self.dataArray[i]];

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

            [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

        }

    }else{

        _isAllSelected = YES;

        [sendersetTitle:@"全选"];

        for(NSIndexPath*indexPathinarr) {

            //根据索引,获取cell 然后就可以做你想做的事情啦

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            cell.accessoryType = UITableViewCellAccessoryCheckmark; //切换为选中


        }

        for(inti =0; i

            [self.selectedArrMaddObject:self.dataArray[i]];

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

        }

    }

    NSLog(@"=====%@", self.selectedArrM);

}

#pragma mark

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

     NSArray*subviews = [[tableViewcellForRowAtIndexPath:indexPath]subviews];

        NSString* name =self.dataArray[indexPath.row];


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果为选中状态

        cell.accessoryType = UITableViewCellAccessoryNone; //切换为未选中

        [self.selectedArrMremoveObject:name];

        [_selectIndexsremoveObject:indexPath];//数据移除

    }else{//未选中

        cell.accessoryType = UITableViewCellAccessoryCheckmark; //切换为选中

        [self.selectedArrMaddObject:name];

        [_selectIndexsaddObject:indexPath];//添加索引数据到数组

    }

    NSLog(@"0000====%@",self.selectedArrM);

}

- (NSMutableArray*)selectIndexs{

    if (_selectIndexs == nil) {

        _selectIndexs = [NSMutableArray array];

    }

    return _selectIndexs;

}

@end

你可能感兴趣的:(iOS UITableViewCell取消与全选状态)