zoukankan      html  css  js  c++  java
  • TableView_编辑 实例代码

    @interface MJViewController () <UITableViewDataSource, UITableViewDelegate>

    {

        NSMutableArray *_persons;

    }

    @end

    @implementation MJViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        

        _persons = [NSMutableArray array];

        for (int i = 0; i<30; i++) {

            Person *p = [[Person alloc] init];

            p.name = [NSString stringWithFormat:@"Person---%d", i];

            p.phone = [NSString stringWithFormat:@"%d", 10000 + arc4random_uniform(10000000)];

            [_persons addObject:p];

        }

    }

    #pragma mark - 数据源方法

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

    {

        return _persons.count;

    }

    #pragma mark 每一行显示怎样的cell(内容)

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

    {

        // 1.定义一个标识

        static NSString *ID = @"cell";

        

        // 2.去缓存池中取出可循环利用的cell

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

        

        // 3.如果缓存中没有可循环利用的cell

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

        }

        

        // 4.设置数据

        // 4.1.取出模型

        Person *p = _persons[indexPath.row];

        

        // 4.2.姓名

        cell.textLabel.text = p.name;

        

        // 4.3.手机

        cell.detailTextLabel.text = p.phone;

        

        return cell;

    }

    #pragma mark - 代理方法

    #pragma mark 当用户提交了一个编辑操作就会调用(比如点击了“删除”按钮)

    // 只要实现了这个方法,就会默认添加滑动删除功能

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

    {

        // 如果不是删除操作,直接返回

        if (editingStyle != UITableViewCellEditingStyleDelete) return;

        

        // 1.删除模型数据

        [_persons removeObjectAtIndex:indexPath.row];

        

        // 2.刷新表格

    //    [tableView reloadData];

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

    }

    #pragma mark 当移动了某一行cell就会调用

    // 只要实现了这个方法,就会默认添加排序功能

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    {

    //    NSLog(@"%d --- %d", sourceIndexPath.row, destinationIndexPath.row);

        

    //    [_persons exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

        

        // 1.取出要拖动的模型数据

        Person *p = _persons[sourceIndexPath.row];

        

        // 2.删除之前行的数据

        [_persons removeObject:p];

        

        // 3.插入数据到新的位置

        [_persons insertObject:p atIndex:destinationIndexPath.row];

    }

    #pragma mark 删除

    - (IBAction)remove:(id)sender {

        // 1.进入编辑模式

    //    self.tableView.editing = YES;

        BOOL result = !self.tableView.isEditing;

        [self.tableView setEditing:result animated:YES];

    }

    @end

  • 相关阅读:
    php 函数ignore_user_abort()
    关于VMAX中存储资源池(SRP)
    VMware Integrated OpenStack (VIO)简介
    云计算服务的三种类型(SaaS、PaaS、IaaS)
    vMware存储:SAN配置基础
    关于不同应用程序存储IO类型的描述
    (转)OpenFire源码学习之十:连接管理(上)
    (转)OpenFire源码学习之九:OF的缓存机制
    (转)OpenFire源码学习之八:MUC用户聊天室
    (转)OpenFire源码学习之七:组(用户群)与花名册(用户好友)
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4159321.html
Copyright © 2011-2022 走看看