UITableView Multi-Select Delete mode

Source: Internet
Author: User

1. Set TableView edit mode to open;

[_tableview Setediting:yes];

2. Set the variable array that receives the cell that is selected to wait for editing, and set whether the BOOL variable record is in edit mode

Nsmutablearray *_waitarray;

BOOL _isedit;

3. When the Toggle Edit Status button is set, it is set to the system's own. Self.editbuttonitem is a dedicated button that comes with the system. The function is to set the editing state of the table (TableView), which can toggle the edit state/non-edit state in its Click event

Self.navigationItem.leftBarButtonItem = Self.editbuttonitem;

-(void) setediting: (BOOL) editing animated: (BOOL) animated
{//System comes with a dedicated button Self.editbuttonitem Click event
Methods that need to overload the parent class call the parent class first
[Super Setediting:editing Animated:yes];
The editing in this method will automatically change
[_tableview setediting:editing Animated:yes];
When changing the editing state, empty the array to be deleted (do not delete after exiting edit mode)
[_watiarray removeallobjects];
Set the private button as a global variable
if (editing) {
Self.navigationItem.rightBarButtonItem = _deletebbi;
} else {
Self.navigationItem.rightBarButtonItem = nil;
}
}

4. Set the edit mode to delete in the function that returns the edit mode type | Insert TableView is set to multiple selection and in edit mode default first click cell to be selected, click again to uncheck in the selected state

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath
{
return Uitableviewcelleditingstyledelete | Uitableviewcelleditingstyleinsert;
}

5. In the cell is selected function, when the cell is selected, first determine whether in edit mode. If it is in edit mode, the selected cell is added to the array of wait operations

If it is not in edit mode, the inverse function is not fired if you want to set the direct jump from selected to unchecked mode.

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
if (tableview.editing) {
In edit mode, add the data of the clicked cell to the array you want to delete.
[_temparr Addobject:[_dataarr ObjectAtIndex:indexPath.row];
}else{
If it's not edit mode, then why do you do it? (unchecked)
[TableView Deselectrowatindexpath:indexpath Animated:yes];
Do not perform an inverse firing function from selected to not selected in non-edit mode
}
}
6. In the cell's inverse function, when the cell is deselected, first determine if it is in edit mode, and if it is in edit mode, the data that is being reversed is removed from the array of wait operations

If you are in non-edit mode, do nothing

-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) Indexpath
{
if (tableview.editing) {//If you are in an edit state, the reversed data is purged from the array to be deleted
[_temparr RemoveObjectAtIndex:indexPath.row];
}else{
NSLog (@ "000");
}
}
7. In the Delete button's Click event, the delete operation is deleted and the list is refreshed.

-(void) Deleteclick
{
Delete the elements contained in the array to be deleted from the data source
[_dataarray Removeobjectsinarray:_waitarray];
Refresh List

[_tableview Reloaddata];
}

UITableView Multi-Select Delete mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.