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

一 核心API


Class: UITableView

Delegate: UITableViewDataSource, UITableViewDelegate

涉及到的API:

插入和删除

1
/** 2 * 让tableView进入或退出编辑状态(TableView方法) 3 */ 4 - (void)setEditing:(BOOL)editing animated:(BOOL)animated; 5 6 /** 7 * 指定哪些行的cell可以进行编辑(UITableViewDataSource协议中方法) 8 */ 9 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 10 11 /** 12 * 指定cell的编辑状态(删除还是插入)(UITableViewDataSource协议中方法) 13 */ 14 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; 15 16 /** 17 * 选中删除(或插入)状态之后的操作(数据源数组进行更新,cell删除或插入)(UITableViewDataSource协议中方法) 18 */ 19 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; 20 21 /** 22 * 插入cell(UITableView方法) 23 */ 24 - (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 25 26 /** 27 * 删除cell(UITableView方法) 28 */ 29 - (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

移动
1 /**
2  *  移动cell后的操作: 数据源进行更新
3  */
4 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
5 
6 /**
7  *  指定tableView哪些行可以移动
8  */
9 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

二 功能实现


(1) 让TableView进入编辑状态

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

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

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

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

三 代码实现


  1 //
  2 //  TableViewController.m
  3 //  UITableViewEdit
  4 //
  5 //  Created by lovestarfish on 15/11/11.
  6 //  Copyright © 2015年 S&G. All rights reserved.
  7 //
  8 
  9 #import "TableViewController.h"
 10 #import "UIImage+Scale.h"
 11 
 12 @interface TableViewController ()
 13 //数据源数组
 14 @property (nonatomic,strong) NSMutableArray *dataSource;
 15 
 16 @end
 17 
 18 @implementation TableViewController
 19 
 20 /**
 21  *  懒加载为数据源数组开辟空间
 22  */
 23 - (NSMutableArray *)dataSource {
 24     if (!_dataSource) {
 25         self.dataSource = [NSMutableArray array];
 26     }
 27     return _dataSource;
 28 }
 29 
 30 - (void)viewDidLoad {
 31     [super viewDidLoad];
 32     //添加系统的编辑按钮
 33     self.navigationItem.rightBarButtonItem = self.editButtonItem;
 34     //读取本地数据
 35     [self readDataFromLocal];
 36 }
 37 
 38 /**
 39  *  从本地plist文件读取数据
 40  */
 41 - (void)readDataFromLocal {
 42     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
 43     NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
 44     self.dataSource = [NSMutableArray arrayWithArray:dic[@"app"]];
 45 }
 46 
 47 #pragma mark - Table view data source
 48 /**
 49  *  返回分区数目
 50  */
 51 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 52     return 1;
 53 }
 54 
 55 /**
 56  *  返回分区的行数
 57  */
 58 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 59     return self.dataSource.count;
 60 }
 61 
 62 /**
 63  *  返回分区的cell
 64  */
 65 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 66     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
 67     //为cell上的控件赋值
 68     cell.imageView.image = [[UIImage imageNamed:self.dataSource[indexPath.row][@"image"]] scaleToSize:CGSizeMake(80, 80)];
 69     cell.textLabel.text = self.dataSource[indexPath.row][@"name"];
 70     
 71     return cell;
 72 }
 73 
 74 /**
 75  *  让tableView进入或退出编辑状态(TableView方法)
 76  */
 77 - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
 78     //先调用父类的方法
 79     [super setEditing:editing animated:animated];
 80     
 81     //使tableView处于编辑状态
 82     [self.tableView setEditing:editing animated:animated];
 83 }
 84 
 85 /**
 86  *  指定哪些行的cell可以进行编辑(UITableViewDataSource协议中方法)
 87  */
 88 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 89     if (0 == indexPath.row) {
 90         return NO; //第一行不能进行编辑
 91     } else {
 92         return YES;
 93     }
 94 }
 95 
 96 #pragma mark 插入&&删除
 97 /**
 98  *  指定cell的编辑状态(删除还是插入)(UITableViewDataSource协议中方法)
 99  */
100 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
101     //不同行,可以设置不同的编辑样式,编辑样式是一个枚举类型
102     if (1 == indexPath.row) {
103         return UITableViewCellEditingStyleInsert; //插入
104     } else {
105         return  UITableViewCellEditingStyleDelete; //删除
106     }
107 }
108 
109 /**
110  *  选中删除(或插入)状态之后的操作(数据源数组进行更新,cell删除或插入)(UITableViewDataSource协议中方法)
111  */
112 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
113     //点击 删除 按钮的操作
114     if (editingStyle == UITableViewCellEditingStyleDelete) {//判断编辑状态为删除时
115         
116         //1. 更新数据源数组: 根据indexPath.row作为数组下标,从数组中删除数据
117         [self.dataSource removeObjectAtIndex:indexPath.row];
118         
119         //2. tableView中 删除一个cell
120         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
121     }
122     
123     //点击 + 号的操作
124     if (editingStyle == UITableViewCellEditingStyleInsert) {//判断编辑状态为插入时
125         
126         //1. 更新数据源: 向数组中添加数据
127         NSDictionary *dic = @{@"name":@"微信",@"image":@"微信"};
128         [self.dataSource insertObject:dic atIndex:indexPath.row];
129         
130         //2. tableView中插入一个cell
131         [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
132     }   
133 }
134 
135 #pragma mark 移动
136 /**
137  *  移动cell后的操作: 数据源进行更新
138  */
139 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
140     //1. 从原位置移除,在原位置移除之前,需要保存一下原位置的数据
141     NSDictionary *dic = self.dataSource[fromIndexPath.row];
142     [self.dataSource removeObjectAtIndex:fromIndexPath.row];
143     
144     //2. 添加到目的位置
145     [self.dataSource insertObject:dic atIndex:toIndexPath.row];
146 }
147 
148 /**
149  *  指定tableView哪些行可以移动
150  */
151 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
152     if (0 == indexPath.row) {
153         return NO; //NO cell不能移动
154     } else {
155         return YES; //YES cell可以移动
156     }
157 }
158 
159 - (void)didReceiveMemoryWarning {
160     [super didReceiveMemoryWarning];
161 }
162 
163 @end
原文地址:https://www.cnblogs.com/lovestarfish/p/4991221.html