• UI3_UITableViewDelete(多选)


    //  AppDelegate.m
    //  UI3_UITableViewDelete(多选)
    //
    //  Created by zhangxueming on 15/7/14.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "ViewController.h"
    
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        ViewController *root = [[ViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
        self.window.rootViewController = nav;
        [self.window makeKeyAndVisible];
        
        return YES;
    }
    
    //
    //  ViewController.h
    //  UI3_UITableViewDelete(多选)
    //
    //  Created by zhangxueming on 15/7/14.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
    
    
    @end
    
    
    //
    //  ViewController.m
    //  UI3_UITableViewDelete(多选)
    //
    //  Created by zhangxueming on 15/7/14.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        UITableView *_tableView;
        //数据源
        NSMutableArray *_dataList;
        //要删除的数据
        NSMutableArray *_removeList;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self creatDataSource];
        [self creatUI];
    }
    
    //创建数据源
    - (void)creatDataSource
    {
        _dataList = [NSMutableArray array];
        _removeList = [NSMutableArray array];
        NSMutableArray *boys = [NSMutableArray array];
        for (int i=0; i<20; i++) {
            NSString *name = [NSString stringWithFormat:@"boy:%d", i+1];
            [boys addObject:name];
        }
        [_dataList addObject:boys];
        
        NSMutableArray *grils = [NSMutableArray array];
        for (int i= 0; i<10; i++) {
            NSString *name = [NSString stringWithFormat:@"gril:%d", i+1];
            [grils addObject:name];
        }
        [_dataList addObject:grils];
    }
    
    //创建UI
    
    - (void)creatUI
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        
        //设置代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        //设置尾视图
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 44)];
        footerView.backgroundColor = [UIColor cyanColor];
        _tableView.tableFooterView = footerView;
        NSLog(@"view = %@", _tableView.tableFooterView);
        //设置头视图
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 44)];
        headerView.backgroundColor = [UIColor yellowColor];
        _tableView.tableHeaderView = headerView;
        
        //添加Btn
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame =CGRectMake(50, 0, footerView.frame.size.width-100,44);
        [btn setTitle:@"remove" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(removeData) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:btn];
        
        //设置编辑按钮
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
        
        
        [self.view addSubview:_tableView];
    
    }
    
    //进入编辑状态
    
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:YES];
        [_tableView setEditing:editing animated:YES];
    }
    
    //Editing
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //同时设置insert 及 delete 自动进入多选状态
        return  UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
    }
    
    //删除数据
    - (void)removeData
    {
        for (int i=0; i<2; i++) {
            NSArray *array = [[_dataList objectAtIndex:i] copy];
            NSInteger rows  = array.count;
            for (int j=0; j<rows; j++) {
                NSString *obj = [array objectAtIndex:j];
                if ([_removeList containsObject:obj]) {
                    [[_dataList objectAtIndex:i] removeObject:obj];
                }
            }
        }
        [_tableView reloadData];
    }
    
    
    #pragma mark   ---UITableView---
    
    //返回分区个数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return _dataList.count;
    }
    
    //返回每个分区有多少行
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [[_dataList objectAtIndex:section] count];
    }
    
    //返回UITableViewCell
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellID = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        return cell;
    }
    
    //选中状态
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        if (tableView.editing) {
            NSString *obj = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
            if (![_removeList containsObject: obj])
            {
                [_removeList addObject:obj];
            }
        }
    }
    
    //结束选中状态
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView.editing) {
            NSString *obj = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
            if ([_removeList containsObject:obj]) {
                [_removeList removeObject:obj];
            }
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
  • 相关阅读:
    UnityVS(Visual Studio Tools For Unity)的安装与使用
    Balsamiq Mockups注册码
    python基础之os.system函数
    jenkins配置记录(1)--添加用户权限
    chromedriver与chrome各版本及下载地址
    高阶面试官应掌握哪些面试技巧
    [面试技巧]16个经典面试问题回答思路
    自动代码质量分析(GitLab+JenKins+SonarQube)
    Git提交代码自动触发JenKins构建项目
    Allure 安装及使用
  • 原文地址:https://www.cnblogs.com/0515offer/p/4645970.html
Copyright © 2020-2023  润新知