TableView编辑模式及自定义右滑多选图标

// 告诉tableView在编辑模式下可以多选


self.tableView.allowsMultipleSelectionDuringEditing = YES;

//进入编辑模式


[self.tableView setEditing:!self.tableView.isEditing animated:YES];

//刷新TableView的方式


- (void)insertRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;//插入一行

-(void)deleteRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;//删除一行

- (void)reloadRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);//更新某一行

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);//移动某一行

//右滑进入编辑模式,单个删除,置顶


-(NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

[self.wineArray removeObjectAtIndex:indexPath.row];

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

}];

UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

XMGWine *wine = self.wineArray[indexPath.row];

[self.wineArray removeObject:wine];

[self.wineArray insertObject:wine atIndex:0];

[self.tableView reloadData];

}];

return @[deleteAction,topAction];

}

//编辑模式多选自定义图片,不用系统的蓝色,在cell中布局时改变


- (void)layoutSubviews{

for (UIControl *control in self.subviews) {

if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]) {

for (UIView *view in control.subviews) {

UIImageView *image = (UIImageView *)view;

if (self.selected) {

image.image = [UIImage imageNamed:@"CellButtonSelected"];

}else{

image.image=[UIImage imageNamed:@"CellButton"];

}

}

}

}

[super layoutSubviews];

}


- (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 *view in control.subviews)

{

if ([view isKindOfClass: [UIImageView class]]) {

UIImageView *image=(UIImageView *)view;

if (!self.selected) {

image.image=[UIImage imageNamed:@"CellButton"];

}

}

}

}

}

}

你可能感兴趣的:(TableView编辑模式及自定义右滑多选图标)