一 核心API


Class: UITableView

Delegate: UITableViewDataSource, UITableViewDelegate

涉及到的API:

插入和删除

 1 /**
* 让tableView进入或退出编辑状态(TableView方法)
*/
- (void)setEditing:(BOOL)editing animated:(BOOL)animated; /**
* 指定哪些行的cell可以进行编辑(UITableViewDataSource协议中方法)
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; /**
* 指定cell的编辑状态(删除还是插入)(UITableViewDataSource协议中方法)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; /**
* 选中删除(或插入)状态之后的操作(数据源数组进行更新,cell删除或插入)(UITableViewDataSource协议中方法)
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; /**
* 插入cell(UITableView方法)
*/
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; /**
* 删除cell(UITableView方法)
*/
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 移动
 /**
* 移动cell后的操作: 数据源进行更新
*/
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath; /**
* 指定tableView哪些行可以移动
*/
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

二 功能实现


(1) 让TableView进入编辑状态

(2) 指定那些行cell可以进行编辑

(3) 指定cell的编辑状态(删除还是插入)

(4) 选中删除(或插入)状态之后的操作(数据源进行更新,cell删除或插入)

(5) 移动cell后的操作: 数据源进行更新

三 代码实现


 //
// TableViewController.m
// UITableViewEdit
//
// Created by lovestarfish on 15/11/11.
// Copyright © 2015年 S&G. All rights reserved.
// #import "TableViewController.h"
#import "UIImage+Scale.h" @interface TableViewController ()
//数据源数组
@property (nonatomic,strong) NSMutableArray *dataSource; @end @implementation TableViewController /**
* 懒加载为数据源数组开辟空间
*/
- (NSMutableArray *)dataSource {
if (!_dataSource) {
self.dataSource = [NSMutableArray array];
}
return _dataSource;
} - (void)viewDidLoad {
[super viewDidLoad];
//添加系统的编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//读取本地数据
[self readDataFromLocal];
} /**
* 从本地plist文件读取数据
*/
- (void)readDataFromLocal {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.dataSource = [NSMutableArray arrayWithArray:dic[@"app"]];
} #pragma mark - Table view data source
/**
* 返回分区数目
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} /**
* 返回分区的行数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
} /**
* 返回分区的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
//为cell上的控件赋值
cell.imageView.image = [[UIImage imageNamed:self.dataSource[indexPath.row][@"image"]] scaleToSize:CGSizeMake(, )];
cell.textLabel.text = self.dataSource[indexPath.row][@"name"]; return cell;
} /**
* 让tableView进入或退出编辑状态(TableView方法)
*/
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//先调用父类的方法
[super setEditing:editing animated:animated]; //使tableView处于编辑状态
[self.tableView setEditing:editing animated:animated];
} /**
* 指定哪些行的cell可以进行编辑(UITableViewDataSource协议中方法)
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ( == indexPath.row) {
return NO; //第一行不能进行编辑
} else {
return YES;
}
} #pragma mark 插入&&删除
/**
* 指定cell的编辑状态(删除还是插入)(UITableViewDataSource协议中方法)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//不同行,可以设置不同的编辑样式,编辑样式是一个枚举类型
if ( == indexPath.row) {
return UITableViewCellEditingStyleInsert; //插入
} else {
return UITableViewCellEditingStyleDelete; //删除
}
} /**
* 选中删除(或插入)状态之后的操作(数据源数组进行更新,cell删除或插入)(UITableViewDataSource协议中方法)
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//点击 删除 按钮的操作
if (editingStyle == UITableViewCellEditingStyleDelete) {//判断编辑状态为删除时 //1. 更新数据源数组: 根据indexPath.row作为数组下标,从数组中删除数据
[self.dataSource removeObjectAtIndex:indexPath.row]; //2. tableView中 删除一个cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} //点击 + 号的操作
if (editingStyle == UITableViewCellEditingStyleInsert) {//判断编辑状态为插入时 //1. 更新数据源: 向数组中添加数据
NSDictionary *dic = @{@"name":@"微信",@"image":@"微信"};
[self.dataSource insertObject:dic atIndex:indexPath.row]; //2. tableView中插入一个cell
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
} #pragma mark 移动
/**
* 移动cell后的操作: 数据源进行更新
*/
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//1. 从原位置移除,在原位置移除之前,需要保存一下原位置的数据
NSDictionary *dic = self.dataSource[fromIndexPath.row];
[self.dataSource removeObjectAtIndex:fromIndexPath.row]; //2. 添加到目的位置
[self.dataSource insertObject:dic atIndex:toIndexPath.row];
} /**
* 指定tableView哪些行可以移动
*/
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if ( == indexPath.row) {
return NO; //NO cell不能移动
} else {
return YES; //YES cell可以移动
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end

iOS开发 -------- UITableView的编辑的更多相关文章

  1. Xamarin iOS开发中的编辑、连接、运行

    Xamarin iOS开发中的编辑.连接.运行 创建好工程后,就可以单击Xamarin Studio上方的运行按钮,如图1.37所示,对HelloWorld项目进行编辑.连接以及运行了.运行效果如图1 ...

  2. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  3. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  4. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  5. iOS开发-UITableView自定义Cell

    UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...

  6. iOS开发UITableView的动画cell

    1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...

  7. iOS开发--UITableView

    -.建立 UITableView  DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  [Data ...

  8. iOS开发UITableView基本使用方法总结1

    UITableView基本使用方法 1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource 2.然后 ...

  9. iOS开发 UITableView之cell

    1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...

随机推荐

  1. c# 类一般在哪里实例化,是在类内、方法内还是其他地方?

    根据情况,你要一个页面内全局的就在类与方法之间实例化,如果一个方法需要使用这个类的对象,就在内部实例化

  2. vue搭建环境并创建项目

    1.>npm install @vue/cli -g 2.创建项目A a.>vue ui b.在弹出的管理界面创建项目 或 a.npm install -g @vue/cli-init b ...

  3. AngularJs ng-change事件/指令(转)

    from:http://blog.csdn.net/u011127019/article/details/52564111 定义和用法 ng-change 指令用于告诉 AngularJS 在 HTM ...

  4. 关于toolchain(工具链)的一点知识

    之前一直觉得toolchain是个高大上的东西,现摘录 uClibc中的FAQ以助理解. A toolchain consists of GNU binutils, the gcc compiler, ...

  5. Jenkins实现自动化部署

    首先登录到服务器,目前我所使用的program-DEV环境所在服务器为:IP     登录密码为:密码 1.基础配置 检查所有环境变量: $ env 2.下载安装包安装 JDK:由于本机器已经安装,这 ...

  6. 【Scala学习之一】 Scala基础语法

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  7. 什么是ASCII

    以下内容是从百度百科学的 1)ASCII(American Standard Code for Information Interchange:美国信息交换标准代码) 2)产生原因 在计算机中,所有的 ...

  8. Lucene 个人领悟 (一)

    在上学的时候就对搜索有着极大地兴趣,图书馆也借了好多的书看过,也用过Python写过爬虫. 有好多人在初步学习Lucene的时候都以为他是一个搜索引擎,或者搜索工具. 在此我要特别强调一下,Lucen ...

  9. Different between MB SD Connect Compact 5 and MB SD C4 Star Diagnostic Tool

    MB SD C4 Star Diagnostic Tool is the professional MB Star Diagnostic Tools for benz cars and trucks. ...

  10. linux 下面压缩、解压.rar文件

    一,解压问题 在网上下东西的时候,经常会遇到.rar后缀的文件,我用tar解压,解压不出,上网找啊找,一直没找到什么合适的工具来压缩和解压.rar后缀的文件,现在我找到了. 二,rar和unrar安装 ...