参考:  http://www.open-open.com/lib/view/open1430008922468.html

- (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 的数组

}

//注释:从 xib 加载 cell 的方式

方法一:

第一步:
[self.collectionView registerNib:[UINib nibWithNibName:@"QGLShareBtnCell" bundle:nil] forCellWithReuseIdentifier:@"QGLShareBtnCell”];
第二步:
QGLShareBtnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QGLShareBtnCell" forIndexPath:indexPath];

方法二:

    QGLIMGroupListCell *cell = (QGLIMGroupListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell= (QGLIMGroupListCell *)[[[NSBundle mainBundle] loadNibNamed:@"QGLIMGroupListCell" owner:self options:nil] lastObject];
}

//另外: 从xib 加载视图 UIView

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"QGLIMGroupListCell" owner:self options:nil] lastObject];

// 获取 view 上的自视图,通过 tag 获取 UIButton *btn  = [view viewWithTag:111];

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

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

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

  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. IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )

    *****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...

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

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

随机推荐

  1. JavaScript基本概念C - 真与假

    真与假 与 c 和 c++ 非常相似, 但与 Java 不同, JS中被认为true或false范围很广.所有对象 (空字符串除外) 和非零数字都被视为 true.空字符串.零.null 和undef ...

  2. javascript——对象的概念——内建对象

    包括内建对象的所有对象都是Object对象的子对象. 1.Array():构建数组的内建构造器函数 例:创建数组方式有两种: 2.Boolean:是对象,与基本数据类型 布尔值 不相同 例:创建Boo ...

  3. javascript——对象的概念——Object(未完)

    http://www.blogjava.net/zkjbeyond/archive/2006/04/16/41336.html javascript中对象只包括属性和方法两种成员.ECMA-262 把 ...

  4. nginx 添加win 服务

    https://jingyan.baidu.com/article/0964eca279aa818285f536a9.html

  5. LAMP 2.4 Apache访问控制

    通过查看日志发现有个IP 恶意攻击你的网址,可以控制这个IP的访问. 打开主配置文件复制模板. vim /usr/local/apache2/conf/httpd.conf 搜索 /Order 复制 ...

  6. bash: telnet: command not found

    //安装telnet服务 yum -y install telnet-server //安装telnet客户端 yum -y install telnet.*

  7. 使用JSONObject类来生成json格式的数据

    JSONObject类不支持javabean转json 生成json格式数据的方式有: 1.使用JSONObject原生的来生成 2.使用map构建json格式的数据 3.使用javabean来构建j ...

  8. 关于A类,B类,C类IP地址的网段和主机数的计算方法

    关于A类,B类,C类IP地址的网段和主机数的计算方法 IP地址是一个32位的二进制数,由四个八位字段组成.每个IP地址包括两部分:一部分为网络标识(网络号),一部分为主机标识(主机号). A类地址前8 ...

  9. 用fontcreator创建了一个半成品的字体

    下效果,哈哈. 为啥说半成品呢?因为只制作了0到9这几个字符,其他的字母.汉字.符号啥的都没有制作,唯一感觉就是字体设计是一个非常有设计感的活儿,而且需要付出很多的精力,尤其是汉字字体,常见的有6k多 ...

  10. Luogu 4777 【模板】扩展中国剩余定理(EXCRT)

    复习模板. 两两合并同余方程 $x\equiv C_{1} \ (Mod\ P_{1})$ $x\equiv C_{2} \ (Mod\ P_{2})$ 把它写成不定方程的形式: $x = C_{1} ...