标签:
1.创建导航栏
2.创建数据源
3.在按钮的编辑响应事件方法中让tableview可以多选可以和可以编辑
4.在按钮删除的响应事件方法中,对选择的数组进行排序后删除,然后设置回不可多选和编辑
//.h文件中继承UITableViewController
// MyTableViewController.h
// UITableView-2-MultiSelect
//
// Created by hehe on 15/9/28.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyTableViewController : UITableViewController
@end
//.m文件中
//
// MyTableViewController.m
// UiTabView2-多选删除
//
// Created by zp7 on 15/9/28.
// Copyright (c) 2015年 zp. All rights reserved.
//
#import "MyTableViewController.h"
@interface MyTableViewController ()
//定义数据源数组
@property (nonatomic) NSMutableArray *dataArray;
@end
@implementation MyTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self costomNavItem];
[self createDataSourse];
//设置分割线的颜色
self.tableView.separatorColor = [UIColor greenColor];
//设置表尾,让空白的分割线消失
self.tableView.tableFooterView = [[UIView alloc]init];
}
#pragma nark -创建简单的数据源
-(void)createDataSourse
{
self.dataArray =[[NSMutableArray alloc] initWithObjects:@"张三",@"李四",@"xx",@"20",@"30",@"40",@"男",@"男",@"女",@"其他",nil];
}
#pragma mark - 定制导航栏
-(void)costomNavItem
{
//设置标题
self.title =@"朋友圈";
//定制右button
UIBarButtonItem *righBtn =[[UIBarButtonItem alloc] initWithTitle:@"多选" style:UIBarButtonItemStylePlain target:self action:@selector(onSelects)];
self.navigationItem.rightBarButtonItem = righBtn;
UIBarButtonItem *leftBtn =[[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(onDelete)];
self.navigationItem.leftBarButtonItem = leftBtn;
}
#pragma mark - 实现导航条按钮方法
-(void)onSelects
{
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = YES;
}
-(void)onDelete
{
//找到选择行的indexpaths
NSArray *selectArray = [self.tableView indexPathsForSelectedRows];
//对数组进行排序
selectArray = [selectArray sortedArrayUsingSelector:@selector(compare:)];
//对数组从后往前删除
for(NSInteger i = selectArray.count-1;i>=0;i--){
NSIndexPath *selIndexPath = selectArray[i];
[self.dataArray removeObjectAtIndex:selIndexPath.row];
}
[self.tableView reloadData];
//允许在编辑模式下可以多选
self.tableView.allowsMultipleSelectionDuringEditing = NO;
//允许在普通模式下可以多选
//self.tableView.allowsMultipleSelection = YES;
self.tableView.editing = NO;
}
#pragma mark - Table view data source
//设置段数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
//设置段中的文字
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" ];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
UITableView-MultiSelect列表视图多选并删除
标签:
原文地址:http://www.cnblogs.com/wanghengheng/p/4844723.html