- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.dataArray = [NSMutableArray arrayWithArray: @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"]];

[self createView];
self.view.backgroundColor = [UIColor greenColor];
}

-(void)createView{
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;

[self.view addSubview:self.tableView];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ce"];
// self.tableView.bounces = 0;
// self.tableView.showsVerticalScrollIndicator = 0;

self.tableView.tableFooterView = [[UIView alloc]init];//没有的不展示 line

UIBarButtonItem *deleteBar = [[UIBarButtonItem alloc]initWithTitle:@"删除" style: UIBarButtonItemStylePlain target:self action:@selector(deleteBtn)];
self.navigationItem.rightBarButtonItem = deleteBar;

UIBarButtonItem *addBar1 = [[UIBarButtonItem alloc]initWithTitle:@"添加1 " style: UIBarButtonItemStylePlain target:self action:@selector(addBtn)];
UIBarButtonItem *addBar2 = [[UIBarButtonItem alloc]initWithTitle:@" 添加2(模式)" style: UIBarButtonItemStylePlain target:self action:@selector(addBtn2)];

self.navigationItem.leftBarButtonItems = @[addBar1,addBar2];
}

//删除(进入编辑模式)
- (void)deleteBtn{
self.isAdd = NO;

static int flog = 1;
flog ^= 1;
if (flog) {
[self.tableView setEditing:YES animated:YES];

}else{
[self.tableView setEditing:NO animated:YES];

}

}
//增加1
- (void)addBtn{

[self.dataArray addObject:@"新增一条"];
//在 <指定行> 添加
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];

}
- (void)addBtn2{
self.isAdd = YES;

static int flog = 1;
flog ^= 1;
if (flog) {
[self.tableView setEditing:YES animated:YES];

}else{
[self.tableView setEditing:NO animated:YES];

}

}
#pragma mark --dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

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

return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ce"];

cell.textLabel.text = self.dataArray[indexPath.row];

return cell;
}

#pragma mark -- delegate 编辑模式(增加/删除/移动)

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

return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

if (self.isAdd) {
return UITableViewCellEditingStyleInsert; //添加

}else{
return UITableViewCellEditingStyleDelete; //删除
}

}

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

NSInteger sourceRow = indexPath.row;
//删除
if (editingStyle == UITableViewCellEditingStyleDelete) {

[self.dataArray removeObjectAtIndex:sourceRow];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
//添加(点击+号按钮)
else if (editingStyle == UITableViewCellEditingStyleInsert){

[self.dataArray insertObject:@"新增" atIndex: sourceRow+1];
//在指定位置插入
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:sourceRow+1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationRight];

}

}

//移动(只有写了这个方法才能移动,但此时不能左滑)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSInteger sourceRow = sourceIndexPath.row;
NSInteger destinRow = destinationIndexPath.row;

NSString *moveObject = [self.dataArray objectAtIndex:sourceRow];

[self.dataArray removeObjectAtIndex:sourceRow];
[self.dataArray insertObject:moveObject atIndex:destinRow]; //改变在<对象>数组中的位置

}

#pragma mark --左滑选项(title可自已定义)
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

//1.删除
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" 删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义
NSLog(@"点击了-删除");

[self.dataArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];

}];
deleteRoWAction.backgroundColor = [UIColor greenColor]; //定义button的颜色,默认是红色的

//test
UITableViewRowAction *test = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" 置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义
NSLog(@"点击了-test");
}];
test.backgroundColor = [UIColor blueColor];

return @[deleteRoWAction,test];//最后返回这俩个RowAction 的数组
}

UITableView 编辑模式(增加-删除-移动---自定义左滑 title)的更多相关文章

  1. UITableView 编辑模式(增加-删除-移动---自定义左滑 title) xib cell

    参考:  http://www.open-open.com/lib/view/open1430008922468.html - (void)viewDidLoad { [super viewDidLo ...

  2. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  3. UITableView编辑模式大全解

    1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...

  4. UITableView编辑模式

    UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...

  5. tableview左滑按钮 tableviewcell自定义左滑按钮

    当我们在使用tableview时,往往需要在cell左滑时显示一个或是多个按钮,但系统默认的只可显示一个,如常见的删除按钮,那么当我们的需求要求要有多个按钮时又该怎么办呢,我们往下看. 首先,现看看系 ...

  6. iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除

    首先创建项目,在storyboard如下布局控件,设置好约束 然后创建cell模型类XMGWineCell数据模型类XMGWine创建UITableView,设置数据源协议,实现数据源方法懒加载数据这 ...

  7. [Xcode 实际操作]五、使用表格-(9)删除UITableView单元格(手势左滑调出删除按钮)

    目录:[Swift]Xcode实际操作 本文将演示如何删除某一行单元格.手势左滑调出删除按钮. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIK ...

  8. Android ListView左滑删除、左滑自定义功能

    最近项目需要ListView左滑删除功能,搜集了很多资料发现了一个某一前辈写的库能很简单的实现这个功能,而且有源码,直接拿来使用了. 库名字叫做SwipeMenuListView,下面给大家演示一下使 ...

  9. iOS UIKit:TableView之编辑模式(3)

    一般table view有编辑模式和正常模式,当table view进入编辑模式时,会在row的左边显示编辑和重排控件,如图 42所示的编辑模式时的控件布局:左边的editing control有表 ...

随机推荐

  1. 如何用C语言封装 C++的类,在 C里面使用

    本文给出了一种方法.基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在  wrapper的实现文件里实现. 1. apple.h #ifnde ...

  2. HTML5 简介、HTML5 浏览器支持

    HTML5是HTML最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定. HTML5的设计目的是为了在移动设备上支持多媒体. HTML5 简单易学. 什么是 HTML5? HTML5 ...

  3. JSP page指令

    JSP page指令: JSP文件: <%@ page language="java"%> <%@ page import="java.util.*&q ...

  4. 部署JForum 2.1.9遇到的问题及解决方法

    1. 主要问题是出在连接数据库和创建表阶段,当我们配置好MySQL的各种参数后,创建表的时候会报错: 原因:主要是由于建表的SQL语句和MySQL的版本不一致导致的. 解决办法:简单来说,在MYSQL ...

  5. POJ1200(hash)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27536   Accepted: 7692 Des ...

  6. Go并发编程实践

    前言 并发编程一直是Golang区别与其他语言的很大优势,也是实际工作场景中经常遇到的.近日笔者在组内分享了我们常见的并发场景,及代码示例,以期望大家能在遇到相同场景下,能快速的想到解决方案,或者是拿 ...

  7. ArcGIS API for JavaScript 4.2学习笔记[2] 显示3D地图

    3D地图又叫场景. 由上一篇可知, require入口函数的第一个参数是字符串数组 ["esri/Map", "esri/views/MapView", &qu ...

  8. 设计社区Dribbble VS. Bēhance,你选谁?

    Behance和Dribbble都是主流的设计作品分享平台,为广大设计师同胞们带来了莫大的便利,所以很多设计师通常两个社区都会关注.很多设计师在展示个人信息的时候,通常也会把这两个平台的链接放到个人资 ...

  9. button快速点击造成多次相应的解决办法

    UIButton+touch.h #import <UIKit/UIKit.h> #define defaultInterval 3 //默认时间间隔 @interface UIButto ...

  10. java.math.BigDecimal()的用法

    Java中简单的浮点数类型float和double是不能进行运算的,不光Java,很多语言都是这样. 我们运行下面程序你将会看到 public class TestMathDecimal { publ ...