//
// 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. repcached的安装练习

    1 自行寻找一个具有大量非结构化数据,很难使用关系型数据库进行处理的场景,清晰描述场景并指出困难所在,要求原创  譬如说:以易迅电商为例,顾客会对购买的商品做出反馈评论,这些评论都是非结构化的数据,如 ...

  2. UESTC 885 方老师买表 --状压DP

    将方格的摆放分成两种: 1.水平摆放:此时所占的两个格子都记为1. 2.竖直摆放:此时底下那个格子记为1,上面那个记为0. 这样的话,每行都会有一个状态表示. 定义:dp[i][s]表示考虑已经填到第 ...

  3. JavaWeb学习之Servlet(四)----ServletConfig获取配置信息、ServletContext的应用

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...

  4. 仙人掌(cactus)

    仙人掌(cactus) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 在冲刺清华集训(THUSC) !于是它开始研究仙人掌,它想来和你一起分享它最近研究的 ...

  5. eclipse点击一个变量使相同名称变量高亮显示的方法

    preferences->java->Editor->Mark Occurences 选择最上的复选框,下面的就有很多了. 其中的Local variables就是变量的高亮显示.

  6. 解决Gradle DSL method not found: ‘android()’

    最近导入as的项目出了这样的问题 这个问题困扰了我很长时间,好吧,搜了半天全都是runProguard的,最后在stackoverflow上搜到解决办法了: http://stackoverflow. ...

  7. Windows Server 2008 DNS服务器安装与配置

    Windows Server 2008 DNS服务器安装与配置本文关键字:Windows Server 2008 DNS.IIS 服务器安装与配置,DNS服务器本文来源:http://www.ip-t ...

  8. JS中的Navigator 对象

    Navigator 对象包含有关浏览器的信息. 很多时候我们需要在判断网页所处的浏览器和平台,Navigator为我们提供了便利 Navigator常见的对象属性如下: 属性 描述 appCodeNa ...

  9. 【夯实Mysql基础】MySQL在Linux系统下配置文件及日志详解

    本文地址 分享提纲: 1. 概述 2. 详解配置文件 3. 详解日志 1.概述 MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于 ...

  10. centos一键优化脚本

    centos一键优化脚本:细节:http://oldboy.blog.51cto.com/2561410/1336488网络状态优化:http://oldboy.blog.51cto.com/2561 ...