码迷,mamicode.com
首页 > 移动开发 > 详细

UI-关于tableView编辑 tableView移动 的代码理解

时间:2015-09-18 23:23:01      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:


  

 

  1 #import "ViewController.h"
  2 
  3 #pragma mark-   编辑的步骤
  4 //UITableView编辑步骤
  5 @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
  6 {//第一步;UITableView有两种编辑状态 默认是删除状态,所以我们要一个容器 保存我们想要的状态
  7     UITableViewCellEditingStyle _editeStyle;
  8     
  9 
 10 }
 11 @property(nonatomic, strong)UITableView *tableView;
 12 @property(nonatomic, strong)NSMutableArray *array;
 13 
 14 @end
 15 
 16 @implementation ViewController
 17 
 18 - (void)viewDidLoad {
 19     [super viewDidLoad];
 20     
 21     NSMutableArray *arr1 = [[NSMutableArray alloc]initWithObjects:@"", @"", @"?是 @"发给", @"到的", @"到的", @"", @"而非", @"二夫人", @"萨达", @"是否", @"", @"生产国", @"沃尔夫", @"而非", @"", @"额外人", @"", @"热热涛", @"恩恩", @"恩恩", nil];
 22     
 23     NSMutableArray *arr2 = [[NSMutableArray alloc]initWithObjects:  @"", @"玩儿", @"确认", @"二人台", @"二二", @"二恶", @"温热", @"曲婉婷", @"太突然", @"日阿斯顿", @"恶趣味", @"巍峨", @"的飞", @"二恶", @"亲爱的风格", @"", @"", @"", @"陈请求", @"恩恩", @"恩恩", @"恩恩", @"恩恩", nil];
 24     
 25     _array = [[NSMutableArray alloc]initWithObjects:arr1, arr2,  nil];
 26     
 27     _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)];
 28     _tableView.delegate = self;
 29     _tableView.dataSource = self;
 30     
 31     [self.view addSubview:_tableView];
 32     //设置左右两边的Titl或者左右两边的按钮,就使用 navigationTitem
 33     //如果想要设置navigationController的样式,使用self.navigationControll.navigationBar
 34     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemEdit) target:self action:@selector(rightBarButtonItemClick:)];
 35     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(leftBarButtonItemClick:)];
 36     
 37     
 38     
 39 }
 40 //返回分区的个数,最好与大的数组的个数相同
 41 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 42     return _array.count;
 43     
 44 }
 45 //返回每个分区的cell的个数
 46 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 47     return [_array[section]count];
 48     
 49 }
 50 //返回 每个分区的头的高度
 51 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
 52     return 50;
 53     
 54 
 55 }
 56 //
 57 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 58    static NSString *str = @"CELL";
 59     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
 60     
 61     if (!cell) {
 62         cell =  [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:str];
 63         
 64     }
 65     cell.textLabel.text = self.array[indexPath.section][indexPath.row];
 66     return cell;
 67     
 68 }
 69 //第二步:在触发编辑的方法里,设定是要插入还是删除 然后让tableView进入该状态
 70 -(void)rightBarButtonItemClick:(UIBarButtonSystemItem *)sender{
 71     //保存我们想要编辑的状态
 72     _editeStyle = UITableViewCellEditingStyleDelete;
 73     //设置tableView的编辑状态为 当前编辑状态的相反状态
 74     [self.tableView setEditing:!self.tableView.isEditing animated:YES];
 75     
 76 
 77 }
 78 //在触发编辑的方法里,设定是要插入还是删除 然后让tableView进入该状态
 79 -(void)leftBarButtonItemClick:(UIBarButtonSystemItem *)sender{
 80     //保存我们想要编辑的状态
 81   _editeStyle = UITableViewCellEditingStyleInsert;
 82     //设置tableView的编辑状态为 当前编辑状态的相反状态
 83 
 84     [self.tableView setEditing:!self.tableView.isEditing
 85                       animated:YES];
 86     
 87 }
 88 //第三步:设置哪一个Cell可以进行编辑,如果不写 默认为都可以
 89 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
 90 //全部可以编辑
 91     return YES;
 92     
 93 
 94 }
 95 //第四步:在进入编辑之前,通过这个方法,返回编编辑的状态(插入和删除)
 96 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
 97 
 98       return _editeStyle;
 99     
100 
101 }
102 //真正进入编辑状态,执行编辑的事情
103 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
104     //处理删除或者插入的过程
105     //判断是要做插入还是删除
106     if (editingStyle == UITableViewCellEditingStyleDelete) {
107         
108         //在编辑的时候一定要先处理数据源,然后在去处理UI
109         //删除分区中的要删除的元素
110         [self.array[indexPath.section] removeObjectAtIndex:indexPath.row];
111        // tableView 删除一个cell的方法 第一个参数代表删除哪一个分区下的cell,第二个代表删除的动画
112         [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
113         
114         
115     }else {
116     
117         NSMutableArray *arr = self.array[indexPath.section];
118         //插入的时候改变数组元素的位置 必须和更新的UI的位置相一致
119         [arr insertObject:@"我的岩岩" atIndex:indexPath.row];
120         NSIndexPath *index = [NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section];
121         [self.tableView insertRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationLeft)];
122         
123     }
124   
125 
126 }
127 #pragma mark  移动部分
128 //第一步:判断是否可以移动cell
129 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
130 
131     return YES;
132     
133 
134 }
135 //第二步:返回移动的目的地
136 //这里可以限制跨区域移动
137 -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
138 {
139 //判断是否是相同的分区
140     if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
141         //如果是则返回可以移动到得目的地
142         return proposedDestinationIndexPath;
143         
144     }
145     else{
146         //不是一个分区的则返回原来的位置
147         return sourceIndexPath;
148         
149     
150     }
151 
152 }
153 //第三步:处理移动结果的代理方法,注意要在编辑状态下才可以移动
154 //写了这个方法之后右边才会出现移动的三道横杠
155 //在这个方法里处理数据移动
156 
157 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
158     //把移动的cell保存到一个 字符串里
159     NSString *str = self.array[sourceIndexPath.section][sourceIndexPath.row];
160     //将这个位置的cell移除
161     [self.array[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
162     //在将保存的字符串(其实就是移除的cell)在插入所要移动的心得位置,注意这个新的位置是由上面返回的位置(不在一个位置则新的位置是原来的位置  如果在一个分区则是则新的位置就是你想要移动到的位置)
163     [self.array[sourceIndexPath.section] insertObject:str atIndex:sourceIndexPath.row];
164     
165 }

 

UI-关于tableView编辑 tableView移动 的代码理解

标签:

原文地址:http://www.cnblogs.com/YDBBK/p/4820598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!