先看效果 :
说下思路:首先要实现tableview的代理。共有几个方法:
1:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete ;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
WEAK_SELF;
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
[tableView setEditing:NO animated:YES];
AddressRecord *record = weakSelf.addressArray[indexPath.row];
[weakSelf deleteAddress:record.id];
}];
deleteRowAction.backgroundColor = ColorRGB(83, 80, 84);
UITableViewRowAction *setDefaultAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"设置默认"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
AddressRecord *record = weakSelf.addressArray[indexPath.row];
[tableView setEditing:NO animated:YES];
[weakSelf setDefaultAddress:record.id];
}];
setDefaultAction.backgroundColor = ColorRGB(169, 166, 170);
AddressRecord *record = self.addressArray[indexPath.row];
if (record.is_default) {
return @[deleteRowAction];
}e