该篇博客是在《iOS高级开发——CollectionView的动态增删cell及模型重构》的基础上继续进行开发的。在之前那篇博客中,我们实现了动态的增删cell,并且使用了模型Model进行重构

来源:互联网 发布:python yield什么意思 编辑:程序博客网 时间:2024/05/19 01:07

   该篇博客是在《iOS高级开发——CollectionView的动态增删cell及模型重构》的基础上继续进行开发的。在之前那篇博客中,我们实现了动态的增删cell,并且使用了模型Model进行重构。今天我们要实现的是动态修改cell中的描述文字,通过这个案例,我们能发现使用Model的好处。代码已经上传至:https://github.com/chenyufeng1991/CollectionView   .中的“动态增加cell和section实现” 。

(1)我这里通过长按cell的操作,弹出输入对话框,然后输入修改的描述文字。CollectionView本身没有长按事件,我这里通过增加长按手势来实现。该功能在《iOS高级开发——CollectionView的cell长按事件实现》中也有说明。实现代码如下:

[objc] view plain copy
 print?
  1. #pragma mark - 创建长按手势  
  2. - (void)createLongPressGesture{  
  3.     
  4.   //创建长按手势监听  
  5.   UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]  
  6.                                              initWithTarget:self  
  7.                                              action:@selector(myHandleTableviewCellLongPressed:)];  
  8.   longPress.minimumPressDuration = 1.0;  
  9.   //将长按手势添加到需要实现长按操作的视图里  
  10.   [self.collectionView addGestureRecognizer:longPress];  
  11. }  

(2)然后需要在长按开始的方法内弹出输入对话框,在点击对话框确定按钮的时候进行修改文本。实现代码如下:

[objc] view plain copy
 print?
  1. - (void) myHandleTableviewCellLongPressed:(UILongPressGestureRecognizer *)gestureRecognizer {  
  2.     
  3.     
  4.   CGPoint pointTouch = [gestureRecognizer locationInView:self.collectionView];  
  5.     
  6.   if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {  
  7.     NSLog(@"长按手势开始");  
  8.       
  9.     NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pointTouch];  
  10.     if (indexPath == nil) {  
  11.       NSLog(@"空");  
  12.     }else{  
  13.       UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入修改后的描述文字" preferredStyle:UIAlertControllerStyleAlert];  
  14.       //以下方法就可以实现在提示框中输入文本;  
  15.       [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {  
  16.         UITextField *cellDescTextField = alertController.textFields.firstObject;  
  17.           
  18.         NSString *cellDesc = cellDescTextField.text;  
  19.         NSLog(@"输入的文字是:%@",cellDesc);  
  20.           
  21.           
  22.         //找到当前操作的section;  
  23.         SectionModel *section = [self.dataSectionArray objectAtIndex:indexPath.section];  
  24.         //找到当前操作的cell;  
  25.         CellModel *cell = [section.cellArray objectAtIndex:indexPath.row];  
  26.         //修改该cell的描述文字;  
  27.         cell.cellDesc = cellDesc;  
  28.           
  29.         //确定当前的cell数组;  
  30.         self.dataCellArray = section.cellArray;  
  31.         //替换cell数组中的内容;  
  32.         [self.dataCellArray replaceObjectAtIndex:indexPath.row withObject:cell];  
  33.         //更新界面;  
  34.         [self.collectionView reloadData];  
  35.           
  36.           
  37.       }]];  
  38.         
  39.       [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];  
  40.       [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {  
  41.         textField.placeholder = @"请输入Section名称";  
  42.       }];  
  43.       [self presentViewController:alertController animated:true completion:nil];  
  44.   
  45.       NSLog(@"Section = %ld,Row = %ld",(long)indexPath.section,(long)indexPath.row);  
  46.         
  47.     }  
  48.   }  
  49.   if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {  
  50.     NSLog(@"长按手势改变,发生长按拖拽动作执行该方法");  
  51.   }  
  52.     
  53.   if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {  
  54.     NSLog(@"长按手势结束");  
  55.   }  
  56. }  


(3)实现效果如下:






     总结,我会持续对CollectionView的使用进行更新,并实现其他复杂实用的效果。请大家不断关注。


最近开源的iOS应用,高仿印象笔记  https://github.com/chenyufeng1991/iOS-Oncenote 。欢迎大家点赞并关注项目进度。也可以安装到手机上试玩哦。

github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

1 0
原创粉丝点击