UI基础 - UITableView 04:对单元格进行编辑(未同步数据源)

■ 对单元格的编辑处理:未同步数据源 

1. 在开发过程中,我们往往需要对 cell 进行操作,比如手机中的通讯录对联系人的增、删、改、查!那么 UItableView 中的 cell 是如何实现该功能呢? 如下

增/删流程

 1 // 第一步:让 tableView 处于编辑状态
 2 -(void)setEditing:(BOOL)editing animated:(BOOL)animated;
 3 
 4 // 第二步:指定 tableView 哪些⾏可以编辑
 5 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
 6 
 7 // 第三步:指定 tableView 编辑的样式:添加、删除...
 8 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
 9 
10 // 第四步:编辑完成(注意先操作数据源,再修改 UI)
11 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath;

拖动流程

 1 // 第一步:让 tableView 处于编辑状态
 2 -(void)setEditing:(BOOL)editing animated:(BOOL)animated; *)indexPath;
 3 
 4 // 第二步:指定 tableView 哪些行可以移动
 5 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(nonnull NSIndexPath *)indexPath;
 6 
 7 // 第三步:监测移动过程,实现限制跨区移动
 8 -(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
 9 
10 // 第四步:移动完成
11 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

2. 下面是对 cell 进行编辑的具体实现

// - ViewController.m

  1 #import "ViewController.h"
  2 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
  3 
  4 @property(nonatomic,strong)UITableView *tableView;
  5 @property(nonatomic,strong)NSMutableArray *dataArray;
  6 
  7 @end
  8 
  9 @implementation ViewController
 10 
 11 - (void)viewDidLoad {
 12     [super viewDidLoad];
 13 
 14     self.navigationController.navigationBar.translucent = YES;
 15     self.title = @"cell编辑";
 16 
 17     // 搞几组硬数据
 18     NSArray *array01 = [NSArray arrayWithObjects:@"白起", @"赵武灵王",@"项羽",@"岳飞",@"兰陵王",@"霍去病",nil];
 19     NSArray *array02 = [NSArray arrayWithObjects:@"汉尼拔",@"凯撒",@"拿破仑",@"薛西斯",@"华莱士",nil];
 20     NSArray *array03 = [NSArray arrayWithObjects:@"雅典娜",@"女娲",@"卡珊德拉",@"嫦娥",@"西王母",@"美杜拉",@"曦和",@"海妖",nil];
 21     self.dataArray = [NSMutableArray arrayWithObjects:array01,array02,array03,nil];
 22 
 23     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
 24     self.tableView.backgroundColor = [UIColor orangeColor];
 25     self.tableView.dataSource = self;
 26     self.tableView.delegate = self;
 27     [self.view addSubview:self.tableView];
 28     self.navigationItem.rightBarButtonItem = self.editButtonItem;
 29 }
 30 
 31 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 32 
 33     static NSString *cellIdentitier = @"cellXXX";
 34     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentitier];
 35     if (!cell) {
 36         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentitier];
 37     }
 38 
 39     cell.textLabel.text = [[self.dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
 40     return cell;
 41 
 42 }
 43 
 44 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 45 
 46     return self.dataArray.count;
 47 }
 48 
 49 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 50 
 51     return   [[self.dataArray objectAtIndex:section] count];
 52 }
 53 
 54 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
 55 
 56     return 40;
 57 }
 58 
 59 
 60 -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
 61 
 62     if (section == 0) {
 63         return @"中国英雄";
 64     }else if (section == 1){
 65         return @"西方勇士";
 66     }else{
 67         return @"女神传说";
 68     }
 69 }
 70 
 71 #pragma mark - 增/删
 72 // 编辑状态
 73 -(void)setEditing:(BOOL)editing animated:(BOOL)animated{
 74     [super setEditing:editing animated:YES];
 75     [self.tableView setEditing:editing animated:YES];
 76 
 77 }
 78 
 79 // 哪些行否允许编辑
 80 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
 81 
 82     return YES;// 只有允许编辑状态,cell 才允许滑动进而出现编辑效果
 83 }
 84 
 85 // 编辑样式
 86 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
 87 
 88     if (indexPath.row == 0 ||  indexPath.row == 1) {
 89         return UITableViewCellEditingStyleDelete;
 90 
 91     }
 92 
 93     return UITableViewCellEditingStyleInsert;
 94 }
 95 
 96 // 编辑完成
 97 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
 98 
 99     // 添加
100     if (editingStyle == UITableViewCellEditingStyleInsert) {
101 
102         // 第 ① 步:处理数据源
103         NSInteger arNum = 200+arc4random()%100;
104         NSString *inserString = [NSString stringWithFormat:@"猜猜我是谁:%ld号",arNum];
105 
106 //        // 效果一:添加在当前行
107 //        NSMutableArray *array = [NSMutableArray arrayWithArray:[self.dataArray objectAtIndex:indexPath.section]];
108 //        [array insertObject:inserString atIndex:indexPath.row];
109 //        [self.dataArray replaceObjectAtIndex:indexPath.section withObject:array];
110 //        // 更新 UI
111 //        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
112 
113 
114         // 效果二:添加在当前行的下一行
115         NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
116         NSMutableArray *array = [NSMutableArray arrayWithArray:[self.dataArray objectAtIndex:indexPath.section]];
117         [array insertObject:inserString atIndex:insertIndexPath.row];
118         [self.dataArray replaceObjectAtIndex:indexPath.section withObject:array];
119         [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:insertIndexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
120     }
121 
122     // 删除
123     if (editingStyle == UITableViewCellEditingStyleDelete) {
124 
125         // 第 ① 步:处理数据源
126         NSMutableArray *array = [NSMutableArray arrayWithArray:[self.dataArray objectAtIndex:indexPath.section]];
127 
128         // 行数大于 1,则一行行地删除
129         if (array.count > 1) {
130             [array removeObjectAtIndex:indexPath.row];
131             [self.dataArray replaceObjectAtIndex:indexPath.section withObject:array];
132             // 更新 UI
133             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
134 
135         // 只 1 行时,删除对应的 section
136         }else{
137             [self.dataArray removeObjectAtIndex:indexPath.section];
138             // 更新 UI
139             [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
140         }
141     }
142 }
143 
144 #pragma mark - 移动
145 // 是否允许移动
146 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
147 
148     return YES;
149 }
150 
151 // cell 想要移动的位置
152 -(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
153 
154     // 同一个 section 允许移动
155     if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
156 
157         return proposedDestinationIndexPath;// 允许移动
158     }
159 
160     return sourceIndexPath;// 不允许移动
161 }
162 
163 // 移动完成
164 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
165     // 获取想要移动的数据
166     NSMutableArray *array = [NSMutableArray arrayWithArray:[self.dataArray objectAtIndex:sourceIndexPath.section]];
167     NSString *nameString = [array objectAtIndex:sourceIndexPath.row];
168 
169     // 添加
170     [array insertObject:nameString atIndex:destinationIndexPath.row];
171 
172     // 删除
173     [array removeObjectAtIndex:sourceIndexPath.row];
174     
175     // 数据源同步
176     [self.dataArray replaceObjectAtIndex:sourceIndexPath.section withObject:array];
177 }
178 
179 @end

运行效果:编辑中

 

 

posted on 2018-04-09 20:39  低头捡石頭  阅读(47)  评论(0编辑  收藏  举报

导航