tableView左滑 编辑 删除或其他操作

来源:互联网 发布:淘宝店主风调色滤镜 编辑:程序博客网 时间:2024/06/06 04:04

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    returnYES; //tableView可编辑

}

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

{

    return@"删除"; //编辑左滑出现文字

}

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

{   //点击删除所进行的操作

    //1. 从数据源中删除

   // [_data removeObjectAtIndex:indexPath.row];

    // 从列表中删除

    //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

   // [tableView endUpdates];


     // 2.从数据源中删除
     [_data removeObjectAtIndex:indexPath.row];
    // 重载数据
    [tableView reloadData];

}

/*   若左滑出现不止一个按钮/做以下操作

-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault

                                                                         title:@"取消收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

                                                                             NSLog(@"收藏点击事件");

                                                                             

                                                                         }];

    

    UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault

                                                                          title:@"顶置" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

                                                                              NSLog(@"顶置按钮点击事件");

                                                                              

                                                                          }];

    

    rowAction.backgroundColor = [UIColor greenColor];

    

    NSArray *arr = @[rowAction,rowAction2];

    return arr;

}

 */


0 0
原创粉丝点击