1. Create a navigation bar
2. Create a data source
3. In the Edit response event method of the button, let TableView can and can edit the multiple selection
4. In the response event method of the button delete, after sorting the selected array, delete, then set back to not multi-select and edit
Inherited Uitableviewcontroller in. h files
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
In the. m file
//
Mytableviewcontroller.m
uitabview2-Multi-Select Delete
//
Created by ZP7 on 15/9/28.
Copyright (c) 2015 ZP. All rights reserved.
//
#import "MyTableViewController.h"
@interface Mytableviewcontroller ()
To define a data source array
@property (nonatomic) Nsmutablearray *dataarray;
@end
@implementation Mytableviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Self costomnavitem];
[Self createdatasourse];
Set the color of a split line
Self.tableView.separatorColor = [Uicolor Greencolor];
Set the footer so that the blank split line disappears
Self.tableView.tableFooterView = [[UIView alloc]init];
}
#pragma nark-Create a simple data source
-(void) Createdatasourse
{
Self.dataarray =[[nsmutablearray alloc] initwithobjects:@ "Zhang San", @ "John Doe", @ "xx", @ "20", @ "30", @ "40", @ "male", @ "male", @ "female", @ "other", NIL];
}
#pragma mark-Custom navigation bar
-(void) Costomnavitem
{
Set Title
Self.title [email protected] "Friends Circle";
Customizing the right button
Uibarbuttonitem *righbtn =[[uibarbuttonitem alloc] initwithtitle:@ "multiple selection" Style:uibarbuttonitemstyleplain target:self Action: @selector (onselects)];
Self.navigationItem.rightBarButtonItem = righbtn;
Uibarbuttonitem *leftbtn =[[uibarbuttonitem alloc] initwithtitle:@ "Delete" Style:uibarbuttonitemstyleplain target:self Action: @selector (OnDelete)];
Self.navigationItem.leftBarButtonItem = leftbtn;
}
#pragma mark-Implement the Navigation bar button method
-(void) onselects
{
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = YES;
}
-(void) OnDelete
{
Find the Indexpaths of the selected row
Nsarray *selectarray = [Self.tableview indexpathsforselectedrows];
Sorting an array
Selectarray = [Selectarray sortedarrayusingselector: @selector (compare:)];
Delete an array from the back and forth
for (Nsinteger i = selectarray.count-1;i>=0;i--) {
Nsindexpath *selindexpath = Selectarray[i];
[Self.dataarray RemoveObjectAtIndex:selIndexPath.row];
}
[Self.tableview Reloaddata];
Allows multiple selections in edit mode
self.tableView.allowsMultipleSelectionDuringEditing = NO;
Allows multiple selections in normal mode
Self.tableView.allowsMultipleSelection = YES;
self.tableView.editing = NO;
}
#pragma mark-table View data source
//Set number of segments
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
return 1;
}
//Set number of rows
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
return self.dataArray.count;
}
//Set text in paragraph
-(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 don't 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 don't want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark-navigation
In a storyboard-based application, you'll 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 List View multi-select and delete