UITableviewCell的花式操作

UITableviewCell花式操作之删除置顶和标记

效果展示

介个是拉出的效果
滑动后侧边栏出现删除、置顶和标记功能
下面直接上代码。。。
对于创建全局数组和创建UITableview这个我就直接跳过了(数组最好是用可变数组)

- (void)viewDidLoad {
    [super viewDidLoad];
    cellArray = [NSMutableArray array];
    for (int i = 0; i < 20; i ++) {
        [cellArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    screeenWidth  = [UIScreen mainScreen].bounds.size.width;
    screenHeight  = [UIScreen mainScreen].bounds.size.height;
    [self makeTableView];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)makeTableView{
    _myTableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return cellArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indetifier  = @"celllllllll";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    }
        cell.textLabel.text = cellArray[indexPath.row];

    return cell;
}

直接上UITableview的代理

//这个是直接添加侧滑栏选项的代理,只支持在8.0之后使用
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{
//    删除选项
    UITableViewRowAction *deleAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [cellArray removeObject:[cellArray objectAtIndex:indexPath.row]];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    deleAction.backgroundColor = [UIColor redColor];
//    置顶选项
    UITableViewRowAction *firstR = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self tableView:tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }];
    firstR.backgroundColor = [UIColor lightGrayColor];
//    标记选项,活着说是特殊操作选项。。。一般是针对数据进行处理
    UITableViewRowAction *markerCell = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"标记未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSString *biaoshiStr = [cellArray objectAtIndex:indexPath.row];
        [cellArray removeObject:biaoshiStr];
        biaoshiStr = [NSString stringWithFormat:@"%@+%@",biaoshiStr,biaoshiStr];
        [cellArray insertObject:biaoshiStr atIndex:indexPath.row];
        [tableView reloadData];
    }];
    markerCell.backgroundColor = [UIColor orangeColor];
    return @[firstR,markerCell,deleAction];
}

然后就添加具体的视线方法,部分方法可以直接在UITableViewRowAction带的block方法块里面视线,但是部分方法需要其它代理的配合
比如说置顶方法

//置顶实现代码
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSString *fromStr = cellArray[sourceIndexPath.row];
    [cellArray removeObject:fromStr];
    [cellArray insertObject:fromStr atIndex:destinationIndexPath.row];
    [tableView reloadData];
}

下面配置一些必要的代理

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

我这是不是就直接把demo代码贴上去了,在贴几个效果图

删除效果展示

介个是删除的效果

标记效果展示

介个是标记的效果

置顶效果展示

介个是置顶的效果
至于标记这个选项,原理就是对数据进行操作,具体怎么操作,根据自己的需求自己考虑,方法就在那里。。。。阿达!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值