iOS开发之UITableView中Cell左滑自定义事件

来源:互联网 发布:女诫 知乎 编辑:程序博客网 时间:2024/04/28 05:05

//自定义左滑操作置顶标记更多等

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0)__TVOS_PROHIBITED{

    UITableViewRowAction *top = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"置顶"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        //数据源置顶

        NSString *tempObject = [listOneArrayobjectAtIndex:indexPath.row];

        [listOneArray removeObject:[listOneArray objectAtIndex:indexPath.row]];

        [listOneArray insertObject:tempObject atIndex:0];

//        [listOneArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

        //表单移动

        //获取顶部的IndexPath

        NSIndexPath *topIndexPath = [NSIndexPathindexPathForRow:0inSection:indexPath.section];

        //

        [tableView moveRowAtIndexPath:indexPathtoIndexPath:topIndexPath];

        //

        [tableView reloadRowsAtIndexPaths:@[topIndexPath]withRowAnimation:UITableViewRowAnimationRight];

        [tableView reloadData];

    }];

    top.backgroundColor = [UIColorredColor];

    

    UITableViewRowAction *bottom = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"置后"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        //数据源置后

        NSString *tempObject = [listOneArrayobjectAtIndex:indexPath.row];

        [listOneArray removeObject:[listOneArray objectAtIndex:indexPath.row]];

        [listOneArray insertObject:tempObject atIndex:listOneArray.count];

        //表单移动

        //获取底部的IndexPath

        NSIndexPath *bottomIndexPath = [NSIndexPathindexPathForRow:listOneArray.count-1inSection:indexPath.section];

        //

        [tableView moveRowAtIndexPath:indexPathtoIndexPath:bottomIndexPath];

        //

        [tableView reloadRowsAtIndexPaths:@[bottomIndexPath]withRowAnimation:UITableViewRowAnimationRight];

        [tableView reloadData];

    }];

    bottom.backgroundColor = [UIColoryellowColor];

    

    UITableViewRowAction *moveUp = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"上移"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        [listOneArrayexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:indexPath.row-1];//交换数据源

        //表单移动

        //获取上一个的IndexPath

        NSIndexPath *upIndexPath = [NSIndexPathindexPathForRow:indexPath.row-1inSection:indexPath.section];

        //

        [tableView moveRowAtIndexPath:indexPathtoIndexPath:upIndexPath];

        //

        [tableView reloadRowsAtIndexPaths:@[upIndexPath]withRowAnimation:UITableViewRowAnimationRight];

        [tableView reloadData];

    }];

    moveUp.backgroundColor = [UIColorgreenColor];

    

    UITableViewRowAction *moveDown = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"下移"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        [listOneArrayexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:indexPath.row+1];//交换数据源

        //表单移动

        //获取下一个的IndexPath

        NSIndexPath *downIndexPath = [NSIndexPathindexPathForRow:indexPath.row+1inSection:indexPath.section];

        //

        [tableView moveRowAtIndexPath:indexPathtoIndexPath:downIndexPath];

        //

        [tableView reloadRowsAtIndexPaths:@[downIndexPath]withRowAnimation:UITableViewRowAnimationRight];

        [tableView reloadData];

    }];

    moveDown.backgroundColor = [UIColorblueColor];

    

    UITableViewRowAction *delete = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"删除"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        [listOneArray removeObjectAtIndex:indexPath.row];

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

    }];

    delete.backgroundColor = [UIColorpurpleColor];

    

    UITableViewRowAction *mark = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"标记"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        //标记点击事件方法

    }];

    mark.backgroundColor = [UIColorblueColor];

    

    

    UITableViewRowAction *more = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"更多"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {

        //更多点击事件方法

    }];

    more.backgroundColor = [UIColorblueColor];

    

    return @[top,bottom,moveUp,moveDown,delete,mark,more];

}


0 0
原创粉丝点击