zoukankan      html  css  js  c++  java
  • iOS开发tableview 的cell 侧滑删除

    网上搜的一些代码超级复杂,这里给大家一个简单的代码, 小白的我不喜欢太复杂的代码。

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        UITableView *demoTab = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

        demoTab.delegate = self;

        demoTab.dataSource = self;

        [self.view addSubview:demoTab];

        

        // Do any additional setup after loading the view, typically from a nib.

    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 1;

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return 10;

    }

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *iden = @"iden";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];

        if (cell==nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];

        }

        cell.textLabel.text = @"删除你啊";

        return cell;

    }

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

    {

        NSLog(@"d");

    }

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

    {

        return YES;

    }

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return UITableViewCellEditingStyleDelete;

    }

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

    {

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

            NSLog(@"删除");

            //[tableView setEditing:YES animated:YES];

        }];

        layTopRowAction1.backgroundColor = [UIColor redColor];

        

        UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

            NSLog(@"更多");

            UIActionSheet *actionSheet = [[UIActionSheet alloc]

                                          initWithTitle:nil

                                          delegate:self

                                          cancelButtonTitle:@"取消"

                                          destructiveButtonTitle:nil

                                          otherButtonTitles:@"创建待办", @"标为未读", @"标为红旗", @"移动", @"这是垃圾邮件",nil];

            actionSheet.actionSheetStyle = UIBarStyleBlackOpaque;

            [actionSheet showInView:self.view];

            [tableView setEditing:YES animated:YES];

        }];

        layTopRowAction2.backgroundColor = [UIColor grayColor];

        NSArray *arr = @[layTopRowAction1,layTopRowAction2];

        return arr;

    }

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

    {

        switch (buttonIndex) {

            case 0:NSLog(@"创建待办");

                break;

            case 1:NSLog(@"标为未读");

                break;

            case 2:NSLog(@"标为红旗");

                break;

            case 3:NSLog(@"移动");

                break;

            case 4:NSLog(@"这是垃圾邮件");

                break;

            case 5:NSLog(@"取消");

                break;

        }

    }

  • 相关阅读:
    好理解的堆排序
    SpringCloud 整合 Python 服务
    SpringCloud入门总结
    Spring异常集中处理和日志集中打印
    Java枚举类型的理解及在后台响应中的使用
    Elasticsearch合并高亮字段
    Elasticsearch分析器结构组成
    Elasticsearch实现英文区分大小写搜索
    Nginx三大功能
    Elasticsearch Java Client 版本区别及起步(5.X 和6.X)
  • 原文地址:https://www.cnblogs.com/godlovexq/p/5126578.html
Copyright © 2011-2022 走看看