码迷,mamicode.com
首页 > 移动开发 > 详细

iOS TableView多选删除理解2

时间:2014-12-24 13:31:41      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:uitableview   多选删除   有个红圆圈   

因为镔哥学习iOS也不是很长时间,所以对很多控件都是一边工作一边学习,现在最近因为项目需求又研究了一下多选删除,其实网上很多这样的demo,但是基本不是纯代码,而且很多方面没有考虑,然后我自己理解上又根基一些demo,自己先了一个,供大家一起学习。

我讲解一下思路就直接代码吧:

思路:一般要实现多选删除

1:前提你要有数据

NSMutableArray *dataArray;//临时用假数据代替

2:你也要有一个存储勾选删除的数据

NSMutableArray *removeList;//勾选时要删除的数据

3:当你勾选删除的时候,你要实现

 [_dataArray removeObjectsInArray:self.removeList];//删除已经勾选的数据

然后重新加载tableview

[tableView reloadData];//重新加载

接着就要清空已经勾选了的存储数据

  [self.removeList removeAllObjects];//清空已经勾选了的数据列表

4:你要实现tableView的代理方法

//编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

//添加一项

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

//取消一项

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath




下面是demo的具体代码,但是我希望大家不要直接复制,先简单理解尝试一遍,别人的东西永远是别人的,你自己尝试写一遍就是你的

//

//  ViewController.m

//  TableView多选删除

//

//  Created by apple on 14/12/9.

//  Copyright (c) 2014 huweibin. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *tableView;

}

@property(nonatomic,strong)NSMutableArray *dataArray;//临时用假数据代替

@property (nonatomic, retain) NSMutableArray *removeList;//勾选时要删除的数据

@property(nonatomic,strong)UIBarButtonItem *rightBtn;


@end


@implementation ViewController


@synthesize removeList;




- (void)viewDidLoad {

    [super viewDidLoad];


    [self initAllData];

   

}


//实现操作

-(void)initAllData

{

    //1:必须首先加载tableview

    tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, self.view.frame.size.height-64)];

    [self.view addSubview:tableView];

    tableView.delegate= self;

    tableView.dataSource = self;

    //2:初始化数据

    _dataArray = [[NSMutableArray alloc] initWithObjects:@"王菲",@"周迅",@"李冰冰",@"白冰",@"紫薇",@"马苏",@"刘诗诗",@"赵薇",@"angelbaby",@"熊黛林",nil];

    self.removeList = [[NSMutableArray alloc]init];

    //3:导航栏设置

    self.view.backgroundColor = [UIColor whiteColor];

    UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];

    [navbar setBackgroundColor:[UIColor clearColor]];

    // 3.设置导航栏文字的主题

    NSShadow *shadow=[[NSShadow alloc]init];

    [shadow setShadowOffset:CGSizeMake(0, 0)];

    [navbar setTitleTextAttributes:@{

                                     NSForegroundColorAttributeName : [UIColor orangeColor],

                                     NSShadowAttributeName : shadow

                                     }];

    //4.创建一个导航栏集合

    UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@"多选删除"];

    

    // 5.设置状态栏样式

    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;


    //6.创建一个右边按钮

        _rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(toggleEdit:)];

    self.navigationItem.rightBarButtonItem = _rightBtn;

     [navItem setRightBarButtonItem:_rightBtn];

    //7.把导航栏集合添加到导航栏中,设置动画关闭

    [navbar pushNavigationItem:navItem animated:NO];

    [self.view addSubview:navbar];

    


    

}


//多选删除操作(其实很简单,重点理解这里)

-(void)toggleEdit:(id)sender {

    [tableView setEditing:!tableView.editing animated:YES];//能出那个??勾选删除

    

    if (tableView.editing)

        [self.navigationItem.rightBarButtonItem setTitle:@"确定"];

    else

    {

        [self.navigationItem.rightBarButtonItem setTitle:@"删除"];

        if (self.removeList.count > 0) {

            [_dataArray removeObjectsInArray:self.removeList];//删除已经勾选的数据

            [tableView reloadData];//重新加载

            [self.removeList removeAllObjects];//清空已经勾选了的数据列表

        }

    }

}


#pragma mark -

#pragma mark Table Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section {

    return [_dataArray count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *DeleteMeCellIdentifier = @"DeleteMeCellIdentifier";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:

                             DeleteMeCellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

                                       reuseIdentifier:DeleteMeCellIdentifier];

    }

    NSInteger row = [indexPath row];

    cell.textLabel.text = [_dataArray objectAtIndex:row];

    return cell;

}

#pragma mark -

#pragma mark Table View Data Source Methods

//编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}

//添加一项

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

{

    NSUInteger row = [indexPath row];

    id addObject = [_dataArray objectAtIndex:row];

    [self.removeList addObject:addObject];

}

//取消一项

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row = [indexPath row];

    id deleteObject = [_dataArray objectAtIndex:row];

    [self.removeList removeObject:deleteObject];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end





iOS TableView多选删除理解2

标签:uitableview   多选删除   有个红圆圈   

原文地址:http://blog.csdn.net/sammyieveo/article/details/42121849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!