当前位置: 首页 > news >正文

ios中tableview的移动添加删除

//
//  MJViewController.m
//  UITableView-编辑模式
//
//  Created by mj on 13-4-11.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

@interface MJViewController () {
    // 当前的编辑模式
    UITableViewCellEditingStyle _editingStyle;
}
@property (nonatomic, retain) NSMutableArray *data;
@end

@implementation MJViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.data = [NSMutableArray array];
    
    for (int i = 0; i<20; i++) {
        NSString *text = [NSString stringWithFormat:@"mj-%i", i];
        [self.data addObject:text];
    }
    
    // 设置tableView可不可以选中
    //self.tableView.allowsSelection = NO;

    // 允许tableview多选
    //self.tableView.allowsMultipleSelection = YES;

    // 编辑模式下是否可以选中
    //self.tableView.allowsSelectionDuringEditing = NO;

    // 编辑模式下是否可以多选
    //self.tableView.allowsMultipleSelectionDuringEditing = YES;

    // 获取被选中的所有行
    // [self.tableView indexPathsForSelectedRows]

    // 获取当前可见的行
    // [self.tableView indexPathsForVisibleRows];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.data = nil;
}

- (void)dealloc {
    [_data release];
    [super dealloc];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
        cell.detailTextLabel.text = @"详细描述";
    }
    
    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法
#pragma mark 设置Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"didSelectRowAtIndexPath");
//}

#pragma mark 提交编辑操作时会调用这个方法(删除,添加)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1.删除数据
        [self.data removeObjectAtIndex:indexPath.row];
        
        // 2.更新UITableView UI界面
        // [tableView reloadData];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        // 添加操作
        
        // 1.添加数据
        int row = indexPath.row + 1;
        [self.data insertObject:@"新添加的数据" atIndex:row];
        
        // 2.更新UI界面
        //[tableView reloadData];
        NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark 决定tableview的编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _editingStyle;
}
#pragma mark 只有实现这个方法,编辑模式中才允许移动Cell
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    // NSLog(@"from(%i)-to(%i)", sourceIndexPath.row, destinationIndexPath.row);
    // 更换数据的顺序
    [self.data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

#pragma mark - 公共方法
#pragma mark 删除数据
- (void)deleteData {
    _editingStyle = UITableViewCellEditingStyleDelete;
    
    // 开始编辑模式
    // self.tableView.editing = YES;
    // [self.tableView setEditing:YES];
    
    BOOL isEditing = self.tableView.isEditing;
    // 开启\关闭编辑模式
    [self.tableView setEditing:!isEditing animated:YES];
}

#pragma mark 添加数据
- (void)addData {
    _editingStyle = UITableViewCellEditingStyleInsert;
    
    BOOL isEditing = self.tableView.isEditing;
    // 开启\关闭编辑模式
    [self.tableView setEditing:!isEditing animated:YES];
}
@end

 

相关文章:

  • 如何写windbg高级脚本---以访问文件的windbg脚本为例说明
  • 部署与管理ZooKeeper(转)
  • 发送队列的默认队列策略 (linux网络子系统学习 第十一节 )
  • hdu 4159 Indomie (DP,数学概率)
  • c++多线程编程之互斥对象(锁)的使用之----死锁
  • 更新整理本人所有博文中提供的代码与工具(C++,2013.10)
  • Entity Framework 教程(转)
  • 连连看游戏(dfs)【华为上机题目】
  • Xcode5 + phoneGap2.9搭建ios开发环境-配置-测试-归档上传/phoneG...
  • linux硬盘分区与格式化
  • 水仙花数
  • 使用Java泛型实现快速排序(快排,Quicksort)算法
  • 必知:嵌入式入门精华书籍推荐
  • 使用json实现android服务器向客户端传数据
  • jquery禁用右键、文本选择功能、复制按键的实现
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • CAP理论的例子讲解
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • HTTP中GET与POST的区别 99%的错误认识
  • 对象管理器(defineProperty)学习笔记
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 聊聊flink的TableFactory
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 思考 CSS 架构
  • 小而合理的前端理论:rscss和rsjs
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 数据可视化之下发图实践
  • #100天计划# 2013年9月29日
  • $.each()与$(selector).each()
  • (1)Nginx简介和安装教程
  • (2)STL算法之元素计数
  • (libusb) usb口自动刷新
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (利用IDEA+Maven)定制属于自己的jar包
  • (排序详解之 堆排序)
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET 材料检测系统崩溃分析
  • /boot 内存空间不够
  • ??eclipse的安装配置问题!??
  • [145] 二叉树的后序遍历 js
  • [20170705]diff比较执行结果的内容.txt
  • [android]-如何在向服务器发送request时附加已保存的cookie数据
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • [bzoj1901]: Zju2112 Dynamic Rankings
  • [C/C++随笔] char与unsigned char区别
  • [C++]高精度 bign (重载运算符版本)
  • [CDOJ 1343] 卿学姐失恋了
  • [CF543A]/[CF544C]Writing Code