自定义删除Cell

tableView.reloadData 

- tableView进行操作的时候分两步

* 1.操作数据(增删改)

* 2.刷新表格

 

- 注意bug:拷贝控件会一同拷贝响应事件

 

自定义删除:(常用)


 

#pragma mark - 数据刷新操作

- (IBAction)remove {

     获得所有被打钩的模型

     点击了删除按钮

    NSMutableArray *deletedWineArray = [NSMutableArray array];

    for (XMGWine *wine in self.wineArray) {

        if (wine.isChecked) {

            [deletedWineArray addObject:wine];

        }

    }

    

     删除所有被打钩的模型

    [self.wineArray removeObjectsInArray:deletedWineArray];

 

     刷新表格

    [self.tableView reloadData];

}


#pragma mark - <代理方法>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

     修改模型

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

    wine.checked = !wine.isChecked;

    

     刷新

    [tableView reloadData];

}

 

局部刷新方法(包括增加/删除/更改):

- (IBAction)remove {

     删除模型数据

    [self.wineArray removeObjectAtIndex:0];

    [self.wineArray removeObjectAtIndex:0];

    

     刷新

    NSArray *indexPaths = @[

                            [NSIndexPath indexPathForRow:0 inSection:0],

                            [NSIndexPath indexPathForRow:1 inSection:0]

                            ];

    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

}

左滑删除方法:(要使用代理)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

     删除模型

    [self.wineArray removeObjectAtIndex:indexPath.row];

    

     刷新

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

 

/**

 *  修改Delete按钮文字为删除

 */

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"删除";

//    return [NSString stringWithFormat:@"删除-%zd", indexPath.row];

}

左滑删除出现两个按钮的方法:(要使用代理)


 

按钮:

- (IBAction)remove {

     删除模型数据

    [self.wineArray removeObjectAtIndex:0];

    [self.wineArray removeObjectAtIndex:0];

    

     刷新

    NSArray *indexPaths = @[

                            [NSIndexPath indexPathForRow:0 inSection:0],

                            [NSIndexPath indexPathForRow:1 inSection:0]

                            ];

    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

}

 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    

}

/**

 *  左滑cell时出现什么按钮

 */

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

{

    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        NSLog(@"点击了关注");

        

         收回左滑出现的按钮(退出编辑模式)

        tableView.editing = NO;

    }];

    

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

要删除的哪一行

        [self.wineArray removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }];

 

编辑模式:


 

 

#pragma mark - 数据刷新操作

- (IBAction)remove {

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

}

#pragma mark - <代理方法>

/**

 *  只要实现了这个方法,左滑出现Delete按钮的功能就有了

 *  点击了左滑出现的Delete按钮会调用这个方法

 */

 点击了删除按钮 调用这个办法 然后删除模型 刷新

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"shabi");

     删除模型

    [self.wineArray removeObjectAtIndex:indexPath.row];

    

     刷新

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

 

/**

 *  修改Delete按钮文字为删除

 */

 先调用这个办法

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"删除");

    return @"删除";

}

 

批量删除:

 


#pragma mark - 数据刷新操作

- (IBAction)multipleRemove {

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

    

    self.removeButton.hidden = !self.tableView.isEditing;

}

 

- (IBAction)remove {

     self.tableView.indexPathsForSelectedRows = [0, 1]

     获得需要删除的模型数据

    NSMutableArray *deletedWineArray = [NSMutableArray array];

    for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {

        [deletedWineArray addObject:self.wineArray[indexPath.row]];

    }

    

     删除模型数据

    [self.wineArray removeObjectsInArray:deletedWineArray];

    

     刷新

    [self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationLeft];

}

 

 

#pragma mark - <代理方法>

/**

 *  只要实现了这个方法,左滑出现Delete按钮的功能就有了

 *  点击了左滑出现的Delete按钮会调用这个方法

 */

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"点击了代理方法");

     删除模型

    [self.wineArray removeObjectAtIndex:indexPath.row];

    

     刷新

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

 

/**

 *  修改Delete按钮文字为删除

 */

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"删除";

}


- 刷新

 带动画的刷新

 reloadRowsAtIndexPaths:要刷新的数组索引

 withRowAnimation:动画类型

[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];


- 删除

带动画的删除

[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0],

                                         [NSIndexPath indexPathForRow:1 inSection:0]]

                      withRowAnimation:UITableViewRowAnimationTop];


- 添加 

带动画的添加

[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]

                    withRowAnimation:UITableViewRowAnimationBottom];

左划删除

 

- 实现数据源方法

#warning 修改delete为删除:1.将模拟器语言改成简体中文  2.为项目添加简体中文

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%s, line = %d, editingStyle= %ld, row = %zd", __FUNCTION__, __LINE__, editingStyle, indexPath.row);

    if (editingStyle == UITableViewCellEditingStyleDelete) {

         1.处理数据

        [self.wines removeObjectAtIndex:indexPath.row];

         2.刷新表格

        [tableView deleteRowsAtIndexPaths:@[indexPath]

                         withRowAnimation:UITableViewRowAnimationTop];

    }

}

 

- 修改按钮文字为汉字

* 1.手机/模拟器设为简体中文

* 2.项目中添加中文支持

![](images/006.png)

 

- 代理方法自定义按钮文字

 该方法return的是左划时候按钮上的具体内容

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);

    return [NSString stringWithFormat:@"--%zd", indexPath.row];

}

左划出现多个按钮iOS8 

- 实现代理方法

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

{

} 

- 数组内元素`UITableViewRowAction`

typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {

    UITableViewRowActionStyleDefault = 0,

    UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,

    UITableViewRowActionStyleNormal

} NS_ENUM_AVAILABLE_IOS(8_0);

 

NS_CLASS_AVAILABLE_IOS(8_0) @interface UITableViewRowAction : NSObject <NSCopying>

 

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

 

@property (nonatomic, readonly) UITableViewRowActionStyle style;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) UIColor *backgroundColor; // default background color is dependent on style

@property (nonatomic, copy) UIVisualEffect* backgroundEffect;

@end

 

- 该方法实现后不会再调用commitEditingStyle的实现

 

- 总结补充:

* cell.contentView左划后,tableView.editing = YES

 

左划出现多个按钮实际开发

 

比较实用的两个第三方

- SWTableViewCell <https://github.com/CEWendel/SWTableViewCell>

- MGSwipeTableCell <https://github.com/MortimerGoro/MGSwipeTableCell>


batchProcessing

- 允许编辑模式下多选

self.tableView.allowsMultipleSelectionDuringEditing = YES;`

 

- 删除所有选中行 

 删除数据

- (IBAction)deleteData {

     1.处理数据

     这个方法能拿到所有的选中cellindexPath

    NSArray *array = [self.tableView indexPathsForSelectedRows];

    

    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];

    for (NSIndexPath *indexPath in array) {

        [arrayM addObject:self.wines[indexPath.row]];

    }

     删除wines中的某些元素

    [self.wines removeObjectsInArray:arrayM];

     2.刷新表格

    [self.tableView reloadData];

    [self.tableView deleteRowsAtIndexPaths:array

                          withRowAnimation:UITableViewRowAnimationTop];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值