//
// 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. HTTP详解2-请求、响应、缓存

    1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干.HTTP协议传输的消息 ...

  2. [转]NPOI TestFunctionRegistry.cs

    本文转自:https://github.com/tonyqus/npoi/blob/master/testcases/main/SS/Formula/TestFunctionRegistry.cs   ...

  3. SpringMVC接收参数的注解笔记

    1.@RequestParam var param = {}; param.keys = delKeys.join();//delKeys是数组,如delKeys=['a',b','c'],join函 ...

  4. 警惕javascript变量的全局污染问题

    作用域的概念总是和变量形影不离,它不是javascript语言独有的概念,只是其运用上与其他大型语言略有不同,JavaScript语言中采用的是弱类型的变量类型,对使用的数据类型未做出严格的要求,是基 ...

  5. 观nginx与lvs负载均衡的较量

    在技术工作者中,常用到的就是lvs负载均衡和Nginx负载均衡了.这两者也是比较普及的.那么,根据不同的需求,两者存在着不同的优势.具体选择哪一个,还要看您的要求了.那么我们在此为大家分享一篇文章,对 ...

  6. 微信公众号开发之LBS

    百度地图Web服务api:http://lbsyun.baidu.com/index.php?title=webapi 1.测距 Route Matrix API v2.0:http://lbsyun ...

  7. C# 程序性能提升篇-1、装箱和拆箱,枚举的ToString浅析

    前景提要: 编写程序时,也许你不经意间,就不知不觉的使程序代码,发生了装箱和拆箱,从而降低了效率,不要说就发生那么一次两次,如果说是程序中发生了循环.网络程序(不断请求处理的)等这些时候,减少装箱和拆 ...

  8. 06SpringMvc_适配器

    适配器的主要功能是去找控制器.Action实现了什么接口 本文案例实现的功能是:在页面上输入中文名字,然后在另外一个网页上显示出来. 案例结构:

  9. Win2008R2配置WebDeploy

    一.配置服务器 1.安装管理服务 2.点击管理服务进行配置 3.安装WebDeploy 3.1通过离线安装包方式安装: https://www.iis.net/downloads/microsoft/ ...

  10. es安装

    1,安装java(至少1.8) yum install -y java java -version 在/etc/profile追加: JAVA_HOME=/usr/java/jdk1..0_45 PA ...