//
// 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. DW与DM

    DW组成部分简介 DW的组成部分有:针对数据源的分析.数据的ETL.数据的存储结构,元数据管理等. 数据源分析 主要是分析要抽取哪些数据,如何抽取(全量还是增量)?它的更新周期是怎么样的?它的数据质量 ...

  2. 【软件使用】用IntelliJ IDEA开发Android程序图解

    博主这里使用的最新的14.0.3版本,下载地址:http://www.jetbrains.com/idea/, 下载后直接安装就可以了. 环境: OS:Win 8 IDE:IntelliJ IDEA ...

  3. 拓扑排序 codevs 4040 cojs 438

    codevs 4040 EZ系列之奖金  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题目描述 Description 由于无敌的WRN在2015年世界英 ...

  4. 边工作边刷题:70天一遍leetcode: day 88-5

    coins in a line I/II/III: check above 1. recursion的返回和dp[left][right]表示什么?假设game是[left,right],那么play ...

  5. python中BeautifulSoup库中find函数

    http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html#contents 简单的用法: find(name, at ...

  6. POJ 1679 The Unique MST --Kruskal应用

    这题可以用次小生成树解,这里用Kruskal算法来做.每条边除维护u,v,w外,还维护: used:表示这条边是否加过 eq:表示有没有与这条边相等的边 del:删除标记,以便删边之用 如果对于一个最 ...

  7. 一个完整的JENKINS下的ANT BUILD.XML文件

    网上看见的,确实很全,该有的基本都覆盖到了.自己拿来稍微改改就可以用了. 注:property中的value是你自己的一些本地变量.需要改成自己的 <?xml version="1.0 ...

  8. 最常用的DOS命令

    ping:利用它可以检查网络是否能够连通,用好它可以很好地帮助我们分析判定网络故障,如ping 127.0.0.1tracert:跟踪路由,查询到相应网站的服务器之间所需经过的路由器个数,如trace ...

  9. docker下部署gitlab

    docker用来隔离应用还是很方便的,一来本身的操作较为简单,二来资源占用也比虚拟机要小得多,三来也较为安全,因为像数据库这样的应用不会再全局暴露端口,同时应用间的通信通过加密和端口转发,更加安全. ...

  10. ping提示小结

    1,Win7 ping 不存在的地址(请求超时) 因为路由器不理睬他. 2,R1-R2-R3 R1有默认路由,R1 ping不存在的地址(目标不可达) 3,R1-R2 R1ping本网段中不存在的地址 ...