iOS开发 -------- UITableView的编辑

时间:2021-04-02 02:41:17

一 核心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