• UITableView02-访淘宝列表的删除样式


    //

    //  MJViewController.m

    //  02-UITableView02-访淘宝列表的删除样式

    //

    //  Created by apple on 13-11-30.

    //  Copyright (c) 2013年 itcast. All rights reserved.

    //

    #import "MJViewController.h"

    #import "Person.h"

     

    @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

  • 相关阅读:
    两道简单的机试题目
    C#二维数组(矩形数组,交错数组)
    用实例展示left Join,right join,inner join,join,cross join,union 的区别
    React 中的 Redux 详解:
    react 组件通信:
    vue 入门, todoList
    Vue 动画
    vue 框架,入门必看
    koa2 的使用方法:(一)
    git 的使用:
  • 原文地址:https://www.cnblogs.com/xgj0721/p/4343561.html
Copyright © 2020-2023  润新知