iOS开发UI篇-tableView在编辑状态下的批量操作(多选)_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > iOS开发UI篇-tableView在编辑状态下的批量操作(多选)

iOS开发UI篇-tableView在编辑状态下的批量操作(多选)

 2016/5/21 5:35:04  赵旭东  程序员俱乐部  我要评论(0)
  • 摘要:先看下效果图直接上代码#import"MyController.h"@interfaceMyController(){UIButton*button;}@property(nonatomic,strong)NSMutableArray*array;//数据源@property(nonatomic,strong)NSMutableArray*selectorPatnArray;//存放选中数据@end@implementationMyController-(void)viewDidLoad
  • 标签:view iOS 开发 操作

先看下效果图

直接上代码

#import "MyController.h"

@interface MyController ()
{
    UIButton *button;
}

@property(nonatomic,strong)NSMutableArray *array;//数据源


@property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据




@end

@implementation MyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加数据源
    for (int i = 0; i < 10; i++) {
        NSString *str = [NSString stringWithFormat:@"第%d行",i + 1];
        [self.array addObject:str];
    }
    
    button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [button setTitle:@"选择" forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    button.frame = CGRectMake(0, 0, 50, 20);
    
    [button addTarget:self action:@selector(selectMore:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *Identifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.textLabel.text = self.array[indexPath.row];
    
    return cell;
}


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


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //选中数据
    [self.selectorPatnArray addObject:self.array[indexPath.row]];
    
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //从选中中取消
    if (self.selectorPatnArray.count > 0) {
        
        [self.selectorPatnArray removeObject:self.array[indexPath.row]];
    }
    
}

#pragma mark - 点击事件

- (void)selectMore:(UIBarButtonItem *)action{
    if ([button.titleLabel.text isEqualToString:@"选择"]) {
        //移除之前选中的内容
        if (self.selectorPatnArray.count > 0) {
            [self.selectorPatnArray removeAllObjects];
        }
        [button setTitle:@"确认" forState:(UIControlStateNormal)];
        //进入编辑状态
        [self.tableView setEditing:YES animated:YES];
    }else{
        
        [button setTitle:@"选择" forState:(UIControlStateNormal)];
     //对选中内容进行操作 NSLog(
@"选中个数是 : %lu 内容为 : %@",(unsigned long)self.selectorPatnArray.count,self.selectorPatnArray); //取消编辑状态 [self.tableView setEditing:NO animated:YES]; } } #pragma mark -懒加载 -(NSMutableArray *)array{ if (!_array) { _array = [NSMutableArray array]; } return _array; } - (NSMutableArray *)selectorPatnArray{ if (!_selectorPatnArray) { _selectorPatnArray = [NSMutableArray array]; } return _selectorPatnArray; }

如果要把tableView在非编辑状态下不让点击,设置下这个属性,就OK了.

@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);  // default is YES. Controls whether rows can be selected when not in editing mode

 如果在某些情况下需要全选,可以按照这个思路:

        for (int i = 0; i < self.array.count; i++) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
            UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:path];
            cell.selected = YES;
            [self.selectorPatnArray addObject:self.array[i]];//添加到选中列表

        }

这只是个人想法,不喜勿喷,欢迎指出不足......

发表评论
用户名: 匿名