//
// RootViewController.m
// UI__TableView的编辑
//
// Created by dllo on 16/3/17.
// Copyright © 2016年 dllo. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()
<
UITableViewDataSource,
UITableViewDelegate
> @property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
self.navigationController.navigationBar.translucent = NO;
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - )];
[self.view addSubview:self.tableView];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[_tableView release]; // 注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
//tableView 的编辑
//右上角添加一个编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//开启编辑模式
//[self.tableView setEditing:YES animated:YES]; //把tableView往下推200
self.tableView.contentInset = UIEdgeInsetsMake(, , , );
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, -, self.view.frame.size.width, )];
imageView.image = [UIImage imageNamed:@"05.jpg"];
imageView.tag = ;
//将图片加到tableView的header
//self.tableView.tableHeaderView = imageView;
[self.tableView addSubview:imageView];
[imageView release];
//打印结构体
NSLog(@"%@", NSStringFromCGRect(self.view.frame));
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//scrollView这儿是TableView
NSLog(@"%@", [scrollView class]);
// 以后在页面里同时存在tableView和scrollView时. 要事先判断当前那个控件触发的协议方法, 避免因为没有判断造成错误的操作
//图片下拉变形
NSLog(@"%f", scrollView.contentOffset.y);
CGFloat yOffset = self.tableView.contentOffset.y;
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:];
if (yOffset < ) {
imageView.frame = CGRectMake(, yOffset, self.view.frame.size.width, -yOffset);
}
}
//重写系统的编辑按钮的点击方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
#pragma mark -- 设置那些行可以编辑, 通过返回yes和no来判断
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
#pragma mark -- 设置编辑的 样式, 默认是删除, 还有添加和多选 insert | delete(多选)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
// 状态只有是delete的时候可以左划出现删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"delete");
// 根据不同的判断实现不同的效果
if (editingStyle == UITableViewCellEditingStyleDelete) {
//要实现删除, 第一件事就是删除对应的数组里面的数据
[self.arr removeObjectAtIndex:indexPath.row];
// [self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
//改变红色区域的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @""; }
// 改变划出来的区域的个数和颜色和文字
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle: title:@"你猜" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
}];
firstAction.backgroundColor = [UIColor yellowColor];
//第二个rowAction
UITableViewRowAction *secAction = [UITableViewRowAction rowActionWithStyle: title:@"我才不猜" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//在block里面, 写对应的点击事件
NSLog(@"");
}];
return @[firstAction, secAction];
}
// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// 1. 现获取要移动的数据
NSString *str = [self.arr[sourceIndexPath.row] retain];
// 2. 把数组里的相应的字符串除掉
[self.arr removeObjectAtIndex:sourceIndexPath.row];
// 3. 把新的字符串插入 到数组
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//注册 新方法
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
cell.textLabel.text = self.arr[indexPath.row];
return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_tableView release];
[_arr release];
[super dealloc];
}
/*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

效果图:

UItableView的编辑--删除移动cell的更多相关文章

  1. UITableView 自带编辑删除 自己定义button

    一:UITableView 自带编辑删除 1:实现两个方法就可以 #pragma mark   tableView自带的编辑功能 -(void)tableView:(UITableView *)tab ...

  2. UITableView的编辑模式

    UITableView可以分普通模式和Editing模式两种,这里我们着重讨论Editing模式,Editing模式中又分三种操作:Insert.Delete. Reallocted.Insert和D ...

  3. UITableView的编辑操作

    继续上篇UITableView和UITableViewController, 打开BNRItemsViewController.m,在类扩展中添加如下属性: @property (nonatomic, ...

  4. iOS开发 -------- UITableView的编辑

    一 核心API Class: UITableView Delegate: UITableViewDataSource, UITableViewDelegate 涉及到的API: 插入和删除 1 /** ...

  5. IOS UITableView多选删除功能

    UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车.收藏列表等. 单行删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除 ...

  6. UI基础:UITableView的编辑和移动

    相对UITableViiew进行编辑,必须设置代理,让代理遵循UITableViewDataSource和UITableViewDelegate协议.因为需要代理实现协议中几个必须的方法. UITab ...

  7. UITableView划动删除的实现

    对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个 ...

  8. ios UITableView多选删除

    第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ...

  9. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

随机推荐

  1. [转]Jquery easyui开启行编辑模式增删改操作

    本文转自:http://www.cnblogs.com/nyzhai/archive/2013/05/14/3077152.html Jquery easyui开启行编辑模式增删改操作先上图 Html ...

  2. sql 入门经典(第五版) Ryan Stephens 学习笔记  第四部分:建立复杂的数据库查询/

    第十三章: 在查询表里结合表 1.等值结合 : // 选择 tabla_a 和table_b 中id相等的行,输出 他们的id 和name select table_a.id , table_a.na ...

  3. Unity-WIKI 之 DebugLine

    脚本功能 在Unity中要使用画线功能,需要添加LineRenderer 组件,或打开Gizmos功能,下面这个组件为开发带来了方便 功能预览 搭建步骤 1.创建“Plugins” 文件夹 2.在“P ...

  4. canvas仿屏幕保护运动线条

    canvas是H5中及其重要的一个新标签,它得出现不仅让前端做图形图表功能变得异常强大,还用极强的性能丰富前端渲染页面的能力. Life is not a problem to be solved, ...

  5. js中接口的声明与实现

    实现接口,必须实现接口里的所有方法. function Interface(name,fns){//声明一个接口类            this.name = name;            th ...

  6. iOS中NSLog的输出格式

    %@ 对象%d %i 整数%u 无符整形%f 浮点/双字%x, %X 二进制整数%o 八进制整数%zu size_t%p 指针 ; NSLog(@"%p",&tem); & ...

  7. js中的垃圾回收机制

    代码回收规则如下: 1.全局变量不会被回收. 2.局部变量会被回收,也就是函数一旦运行完以后,函数内部的东西都会被销毁. 3.只要被另外一个作用域所引用就不会被回收  (闭包)

  8. 26Mybatis_一级缓存及其测试

    这篇文章讲解一级缓存: 先介绍一级缓存的原理:

  9. MVC权限验证之ActionFilterAttribute

    参考:http://www.cnblogs.com/waitingfor/archive/2011/12/27/2303784.html ActionFilterAttribute是Action过滤类 ...

  10. RDLC系列之三 图片显示

    一.头像效果