Some lists often need to edit the function of multi-select, and UITableView with multi-select Delete function, easy to use, do not need to do the data storage and check the state transition, can reduce a lot of development time. The following is an introduction to the use of the UITableView multi-Select.
Effect:
Uitableviewcelleditingstyle
There are three modes of editing State Uitableviewcelleditingstyle:
- Uitableviewcelleditingstyledelete
- Uitableviewcelleditingstyleinsert
- Uitableviewcelleditingstylenone
Multi-marquee style, just need to include uitableviewcelleditingstyledelete and Uitableviewcelleditingstyleinsert on the style.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;}
Then set the status of the UITableView Editing , you can turn on the TableView multi-select function.
Benefits of using your own editing state to do multiple-selection functions
- It is not necessary to record the selected array in the data source, it can be obtained by means of the
indexPathsForSelectedRows selected cell, and it is much more convenient to do the delete operation.
- Enter to exit the editing state for convenience, there are also self-brought animation. PS: When customizing the cell, the child view should be loaded on the contentview of the cell.
- In the case of low requirements, the self-brought icon can meet the needs.
Let's start with the full selection and all-in-one operation, the following are all in single-section cases
- It's easy to do a full selection, traverse through the data source and select each one.
[self.listData enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone]; }];
- All do not choose, reload can become all not selected, if you do not want to reload such a simple rough, you can also remove the currently selected Indexpath array, traverse inverse selection is also possible.
[self.tableView reloadData]; /** 遍历反选 [[self.tableView indexPathsForSelectedRows] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [self.tableView deselectRowAtIndexPath:obj animated:NO]; }]; */
- Multiple sections are similar, note the point when deleting an operation, delete the entire section from the cell to the section to the last one.
Edit operation, below to delete as an example
Nsmutableindexset *insets = [[Nsmutableindexset alloc] init]; [[Self.tableview Indexpathsforselectedrows] enumerateobjectsusingblock:^ (Nsindexpath * _nonnull obj,Nsuinteger idx,BOOL * _nonnull stop) {[insets addIndex:obj.row];}]; [self.listdata removeobjectsatindexes:insets]; [self.tableview deleterowsatindexpaths:[self.tableView Indexpathsforselectedrows] Withrowanimation:uitableviewrowanimationfade]; /** data vacuuming Cancel Edit Status */if (0) { Self.navigationItem.rightBarButtonItem.title = @ "edit"; [self.tableview setediting:no animated: yes]; [self showeitingview:no]; /** with MJ Refresh control reset state [Self.tableView.footer Resetnomoredata]; [Self.tableview Reloaddata]; */ }
Cell Click on the effect of the problem, some people will think that the blue background is very difficult to see, want to remove, can be in the custom cell inside:
self.multipleSelectionBackgroundView = [UIView new];
You can also modify the color of the selected icon, and the color of the other default image will be changed.
self.tintColor = [UIColor redColor];
Effect:
If you do not want to use the default icon, you can also customize:
-(void) layoutsubviews{for (Uicontrol *controlInchSelf.subviews) {if ([Control Ismemberofclass:Nsclassfromstring (@ "Uitableviewcelleditcontrol")]) {for (UIView *vIn Control.subviews) {if ([v Iskindofclass: [UiimageviewClass]]) {Uiimageview *img= (Uiimageview *) v;if (self.selected) {img.image=[UIImage imagenamed:@ "Xuanzhong_icon"]; }else {img.image=[UIImage imagenamed:@ "Weixuanzhong_icon"]; } } } } } [Super Layoutsubviews];}Fit the first picture to empty case-(void) Setediting: (BOOL) editing animated: (BOOL) animated{[Super Setediting:editing animated:animated];for (uicontrol *control in self.subviews) {if ([Control Ismemberofclass: nsclassfromstring (@ "Uitableviewcelleditcontrol")]) {for (uiview *v in Control.subviews) {if ([v iskindofclass: [UIImageView class]]) {uiimageview *img= ( Uiimageview *) v; if (! self.selected) {Img.image=[uiimage imagenamed:@ " Weixuanzhong_icon "]; } } } } } }
Effect:
Demo Address: Https://github.com/yxsufaniOS/TableViewMutableSelectDemo
Links: https://www.jianshu.com/p/a6e4cb42dd03
Multiple options for IOS UITableView