IOS UITableView (multiple options)

Source: Internet
Author: User
Tags uicontrol

IOS UITableView (multiple options)

You often need to edit multiple-choice functions for some lists. UITableview provides the multiple-choice deletion function, which is easy to use. You do not need to store data or switch the selected status by yourself, which reduces the development time. The following describes how to use UITableView multiple options.
Effect:

UITableViewCellEditingStyle

The Edit status UITableViewCellEditingStyle has three modes:

  • UITableViewCellEditingStyleDelete
  • UITableViewCellEditingStyleInsert
  • UITableViewCellEditingStyleNone

The style of the Multi-choice box only needs to include UITableViewCellEditingStyleDelete and UITableViewCellEditingStyleInsert at the same time.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;}

Then set the UITableviewEditingStatus to enable the multiple choice function of tableview.

Benefits of using the built-in editing status for multi-Choice
  • You do not need to record the selected array in the data source.indexPathsForSelectedRowsMethod to obtain the selected cell, which is much easier to delete.
  • It is easy to exit the editing status, and there are built-in animations. Ps: When customizing a cell, the Child view should be loaded on the cell's contentView.
  • If the requirements are not high, the built-in icons can meet the requirements.
Next, let's talk about all-selected and all-unselected operations. The following are all in the case of a single section.
  • It is also easy to select all. traverse the data source and select each item.
[self.listData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];        }];
  • If you do not select all, reload can be changed to the All-unselected status. If you do not want to reload this simple and crude, you can also retrieve the currently selected indexpath array and traverse the reverse selection.
[Self. tableView reloadData];/** traversal invert Selection [[self. tableView indexPathsForSelectedRows] enumerateObjectsUsingBlock: ^ (NSIndexPath * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {[self. tableView deselectRowAtIndexPath: obj animated: NO];}]; */
  • Multiple sections are similar. When deleting a section, you must Delete the entire section when deleting the cell to the last section.
Edit operation. The following example uses deletion 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];/** unedit status when data is cleared */if (self. listData. count = 0) {self. navigationItem. rightBarButtonItem. title = @ "edit"; [self. tableView setEditing: NO animated: YES]; [self showEitingView: NO];/** display the reset status of the control with MJ refresh [self. tableView. footer resetNoMoreData]; [self. tableView reloadData]; */}
Some people may find that the blue background is ugly. To remove it, you can go to the custom Cell:
self.multipleSelectionBackgroundView = [UIView new];

You can also change the color of the selected icon,

self.tintColor = [UIColor redColor];

Effect:


If you do not want to use the default icon, you can also customize it in:

-(Void) layoutSubviews {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: @ "xuanzhong_icon"];} else {img. image = [UIImage imageNamed: @ "weixuanzhong_icon"] ;}}} [Super layoutSubviews];} // adapts to the condition that the first image is null-(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


Link: https://www.jianshu.com/p/a6e4cb42dd03

Related Article

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.