一 核心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. activiti 项目变更控制器

    package com.xinwei.process.controller; import java.util.Calendar; import java.util.HashMap; import j ...

  2. Oracle查询表占用空间的大小

    select * from (select OWNER, segment_name, segment_type, sum(bytes) mmm from dba_segments where /*ta ...

  3. caffe的model参数解析numpy多维数组的存取

    在caffe的参数进行Python解析时,需要对模型的wight和bias的参数进行解析,为了提高结果解析的可读性,需要用numpy将解析的文件进行保存 此时用到np.savetxt方法和np.sav ...

  4. Unity3d之表情动画--眨眼

    可通过BlendShape来实现眨眼动画,效果如下: 转载请注明出处:http://www.cnblogs.com/jietian331/p/7054673.html 代码如下: using Unit ...

  5. 《大话设计模式》c++实现 工厂模式

    工厂模式 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客户端 ...

  6. CSS背景与边框属性-----box-shadow

    box-shadow:none | <shadow> [ , <shadow> ]*   <shadow> = inset? && <leng ...

  7. Swift之关键字使用(I)

    static和class的使用 static 使用 在非class的类型(包括enum和struct)中,一般使用static来描述类型作用域.在这个类型中,我们可以在类型范围中声明并使用存储属性,计 ...

  8. Qt Md5应用示例

    [1].cpp文件 #include "widget.h" #include "ui_widget.h" #include <QCryptographic ...

  9. redis相关问题

    什么是Redis?Redis 是一个使用 C 语言写成的,开源的 key-value 数据库..和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表 ...

  10. tensorflow学习4-过拟合-over-fitting

    过拟合: 真实的应用中,并不是让模型尽量模拟训练数据的行为,而是希望训练数据对未知做出判断. 模型过于复杂后,模型会积极每一个噪声的部分,而不是学习数据中的通用 趋势.当一个模型的参数比训练数据还要多 ...