iOS开发 iOS8系统新出cell侧滑View

本文介绍如何在iOS应用中实现UITableView的侧滑删除功能,并添加置顶及取消关注等自定义操作。通过使用UITableViewRowAction类,可以在iOS8及以上版本轻松实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


侧滑删除、置顶、取消关注,在iOS8之前需要我们自定义,iOS8时苹果公司推出了新的API,UITableViewRowAction类

1.OC版本

// 必须写的方法(否则iOS 8无法删除,iOS 9及其以上不写没问题),和editActionsForRowAtIndexPath配对使用,里面什么不写也行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

// 添加自定义的侧滑功能
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 添加一个删除按钮
    
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 先移除数据源数据
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 再动态刷新UITableView
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        NSLog(@"删除按钮");
    }];
    
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"置顶按钮");
        
    }];
    /// 设置按钮颜色,Normal默认是灰色的,Default默认是红色的
    topRowAction.backgroundColor = [UIColor orangeColor];
    
    UITableViewRowAction *cancelRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"取消关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"取消关注按钮");
    }];
    
    return @[deleteRowAction,topRowAction,cancelRowAction];
}



只是一个删除按钮,而不显示其他的简单的写法,这个也可以用于IOS7,没有测过

// 只是一个删除按钮
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

// 删除的处理
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请三思而行再删除" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.tableView setEditing:NO animated:YES];
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        // 先移除数据源数据
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 再动态刷新UITableView
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}


2.swift版本

// 和editActionsForRowAtIndexPath配对使用
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    }
    
    // iOS8 才有此方法
    // 这里可以添加任意多个操作。要确保这个代码生效,还是需要实现commitEditingStyle这个方法,哪怕commitEditingStyle里面什么也不处理
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "删除") { (UITableViewRowAction, NSIndexPath) -> Void in
            // 先移除数据源数据
            self.dataArray.removeObjectAtIndex(indexPath.row)
            // 再动态刷新UITableView
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
        }
        
        let topRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "置顶") { (UITableViewRowAction, NSIndexPath) -> Void in
            println("置顶按钮点击了")
            // 先移除数据源数据
            var tempObject: AnyObject = self.dataArray.objectAtIndex(indexPath.row)
            self.dataArray.removeObjectAtIndex(indexPath.row)
            self.dataArray.insertObject(tempObject, atIndex: 0)
            self.tableView.reloadData()
            //self.tableView.reloadData()
        }
        topRowAction.backgroundColor = UIColor.orangeColor()
        
        let cancelRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "取消关注") { (UITableViewRowAction, NSIndexPath) -> Void in
            println("取消关注按钮点击了")
            self.navigationController?.popToRootViewControllerAnimated(true)
        }
        
        if indexPath.row == 0 {
            return [deleteRowAction,cancelRowAction]
        }else {
            return [deleteRowAction,topRowAction,cancelRowAction]
        }
        
    }


效果如图:



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值