1.实现单元格的删除,实现效果如下

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. //设置导航栏
  5. self.editButtonItem.title = @"编辑";
  6. self.navigation.rightBarButtonItem = self.editButtonItem;
  7. [self initTableViewData];
  8. // Do any additional setup after loading the view.
  9. }
  10. - (void)didReceiveMemoryWarning
  11. {
  12. [super didReceiveMemoryWarning];
  13. // Dispose of any resources that can be recreated.
  14. }
  15. -(void)initTableViewData{
  16. NSBundle *bundle = [NSBundle mainBundle];
  17. NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];
  18. dataArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
  19. }
  20. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  21. {
  22. return [dataArr count];
  23. }
  24. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  25. {
  26. static NSString *CellIdentifier = @"tableCell";
  27. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  28. NSUInteger row = [indexPath row];
  29. NSDictionary *rowDict = [dataArr objectAtIndex:row];
  30. cell.textLabel.text =  [rowDict objectForKey:@"itemName"];
  31. NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);
  32. NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
  33. cell.imageView.image = [UIImage imageNamed:imagePath];
  34. NSLog(@"cell.image.image  =  %@",imagePath);
  35. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  36. return cell;
  37. }
  38. //选中Cell响应事件
  39. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  40. [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
  41. NSUInteger row = [indexPath row];
  42. NSDictionary *rowDict = [dataArr objectAtIndex:row];
  43. NSString *userName =  [rowDict objectForKey:@"itemName"];
  44. NSLog(@"userName=%@",userName);
  45. }
  46. //返回编辑状态的style
  47. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
  48. editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  49. {
  50. //UITableViewCellEditingStyleInsert
  51. //    return UITableViewCellEditingStyleNone;
  52. return UITableViewCellEditingStyleDelete;
  53. }
  54. //完成编辑的触发事件
  55. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  56. forRowAtIndexPath:(NSIndexPath *)indexPath
  57. {
  58. if (editingStyle == UITableViewCellEditingStyleDelete)
  59. {
  60. [dataArr removeObjectAtIndex: indexPath.row];
  61. //        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  62. //                         withRowAnimation:UITableViewRowAnimationFade];
  63. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  64. withRowAnimation:UITableViewRowAnimationFade];
  65. [tableView reloadData];
  66. }
  67. }
  68. //UIViewController生命周期方法,用于响应视图编辑状态变化
  69. - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
  70. [super setEditing:editing animated:animated];
  71. [self.tableView setEditing:editing animated:YES];
  72. if (self.editing) {
  73. self.editButtonItem.title = @"完成";
  74. } else {
  75. self.editButtonItem.title = @"编辑";
  76. }
  77. }
  78. @end
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. //设置导航栏
  5. self.editButtonItem.title = @"编辑";
  6. self.navigation.rightBarButtonItem = self.editButtonItem;
  7. [self initTableViewData];
  8. // Do any additional setup after loading the view.
  9. }
  10.  
  11. - (void)didReceiveMemoryWarning
  12. {
  13. [super didReceiveMemoryWarning];
  14. // Dispose of any resources that can be recreated.
  15. }
  16.  
  17. -(void)initTableViewData{
  18. NSBundle *bundle = [NSBundle mainBundle];
  19. NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];
  20. dataArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
  21. }
  22.  
  23. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  24. {
  25. return [dataArr count];
  26. }
  27.  
  28. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  29. {
  30. static NSString *CellIdentifier = @"tableCell";
  31. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  32.  
  33. NSUInteger row = [indexPath row];
  34. NSDictionary *rowDict = [dataArr objectAtIndex:row];
  35. cell.textLabel.text = [rowDict objectForKey:@"itemName"];
  36. NSLog(@"cell.label.text = %@",[rowDict objectForKey:@"itemName"]);
  37.  
  38. NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
  39. cell.imageView.image = [UIImage imageNamed:imagePath];
  40. NSLog(@"cell.image.image = %@",imagePath);
  41.  
  42. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  43.  
  44. return cell;
  45. }
  46.  
  47. //选中Cell响应事件
  48. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  49. [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
  50. NSUInteger row = [indexPath row];
  51. NSDictionary *rowDict = [dataArr objectAtIndex:row];
  52. NSString *userName = [rowDict objectForKey:@"itemName"];
  53. NSLog(@"userName=%@",userName);
  54. }
  55.  
  56. //返回编辑状态的style
  57. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
  58. editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  59. {
  60. //UITableViewCellEditingStyleInsert
  61. // return UITableViewCellEditingStyleNone;
  62. return UITableViewCellEditingStyleDelete;
  63. }
  64. //完成编辑的触发事件
  65. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  66. forRowAtIndexPath:(NSIndexPath *)indexPath
  67. {
  68. if (editingStyle == UITableViewCellEditingStyleDelete)
  69. {
  70. [dataArr removeObjectAtIndex: indexPath.row];
  71. // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  72. // withRowAnimation:UITableViewRowAnimationFade];
  73. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  74. withRowAnimation:UITableViewRowAnimationFade];
  75. [tableView reloadData];
  76. }
  77. }
  78. //UIViewController生命周期方法,用于响应视图编辑状态变化
  79. - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
  80. [super setEditing:editing animated:animated];
  81.  
  82. [self.tableView setEditing:editing animated:YES];
  83. if (self.editing) {
  84. self.editButtonItem.title = @"完成";
  85. } else {
  86. self.editButtonItem.title = @"编辑";
  87. }
  88. }
  89. @end

2.移动单元格

  1. //完成移动的触发事件,不添加该方法不实现移动功能
  2. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath
  3. toIndexPath:(NSIndexPath *)destinationIndexPath
  4. {
  5. NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row];
  6. [dataArr removeObjectAtIndex:sourceIndexPath.row];
  7. [dataArr insertObject:item atIndex:destinationIndexPath.row];
  8. }
  1. //完成移动的触发事件,不添加该方法不实现移动功能
  2. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath
  3. toIndexPath:(NSIndexPath *)destinationIndexPath
  4. {
  5. NSDictionary *item = [dataArr objectAtIndex:sourceIndexPath.row];
  6. [dataArr removeObjectAtIndex:sourceIndexPath.row];
  7. [dataArr insertObject:item atIndex:destinationIndexPath.row];
  8. }

3.添加单元格。下面是自定义触发事件,即单击左下角的add按钮

  1. - (IBAction)addistItem:(UIBarButtonItem *)sender {
  2. AppUtils *appUtils = [AppUtils alloc];
  3. //需要先初始化一个UIAlertView
  4. UIAlertView *alert = [UIAlertView alloc];
  5. [appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{
  6. //得到输入框
  7. UITextField *textField=[alert textFieldAtIndex:0];
  8. //        不要写成NSMutableDictionary *newItem = [NSDictionary dictionary];
  9. NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
  10. [newItem setObject:textField.text forKey:@"itemName"];
  11. [newItem setObject:@"1.jpeg" forKey:@"itemImagePath"];
  12. [dataArr addObject:newItem];
  13. [self.tableView reloadData];
  14. })];
  15. }
  1. - (IBAction)addistItem:(UIBarButtonItem *)sender {
  2. AppUtils *appUtils = [AppUtils alloc];
  3. //需要先初始化一个UIAlertView
  4. UIAlertView *alert = [UIAlertView alloc];
  5. [appUtils showInputDialogWithTitle:@"add" message:@"please input new user name:" toAlertView:alert confirmAction:(^{
  6. //得到输入框
  7. UITextField *textField=[alert textFieldAtIndex:0];
  8. // 不要写成NSMutableDictionary *newItem = [NSDictionary dictionary];
  9. NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
  10. [newItem setObject:textField.text forKey:@"itemName"];
  11. [newItem setObject:@"1.jpeg" forKey:@"itemImagePath"];
  12. [dataArr addObject:newItem];
  13. [self.tableView reloadData];
  14. })];
  15. }

4.附上·AppUtils类

  1. #import "AppUtils.h"
  2. #include "RIButtonItem.h"
  3. #include "UIAlertView+Blocks.h"
  4. @implementation AppUtils
  5. //弹出警告框,并实现警告框按钮的触发事件
  6. - (void)showInputDialogWithTitle:(NSString *)title message:(NSString *)message toAlertView:(UIAlertView*) alert confirmAction:(void(^)(void))action{
  7. RIButtonItem* cancelItem = [RIButtonItem item];
  8. cancelItem.label = @"No";
  9. cancelItem.action = ^
  10. {
  11. //为NO时的处理
  12. UITextField *tf=[alert textFieldAtIndex:0];
  13. NSLog(@"UITextField=%@",tf.text);
  14. };
  15. RIButtonItem* confirmItem = [RIButtonItem item];
  16. confirmItem.label = @"Yes";
  17. //    confirmItem.action = action;
  18. alert = [alert initWithTitle:title
  19. message:message
  20. cancelButtonItem:cancelItem
  21. otherButtonItems:confirmItem, nil];
  22. alert.alertViewStyle = UIAlertViewStylePlainTextInput;
  23. confirmItem.action = action;
  24. [alert show];
  25. }
  26. @end
  1. #import "AppUtils.h"
  2. #include "RIButtonItem.h"
  3. #include "UIAlertView+Blocks.h"
  4.  
  5. @implementation AppUtils
  6.  
  7. //弹出警告框,并实现警告框按钮的触发事件
  8. - (void)showInputDialogWithTitle:(NSString *)title message:(NSString *)message toAlertView:(UIAlertView*) alert confirmAction:(void(^)(void))action{
  9. RIButtonItem* cancelItem = [RIButtonItem item];
  10. cancelItem.label = @"No";
  11. cancelItem.action = ^
  12. {
  13. //为NO时的处理
  14. UITextField *tf=[alert textFieldAtIndex:0];
  15. NSLog(@"UITextField=%@",tf.text);
  16. };
  17.  
  18. RIButtonItem* confirmItem = [RIButtonItem item];
  19. confirmItem.label = @"Yes";
  20. // confirmItem.action = action;
  21. alert = [alert initWithTitle:title
  22. message:message
  23. cancelButtonItem:cancelItem
  24. otherButtonItems:confirmItem, nil];
  25.  
  26. alert.alertViewStyle = UIAlertViewStylePlainTextInput;
  27.  
  28. confirmItem.action = action;
  29. [alert show];
  30. }
  31. @end

IOS之表视图单元格删除、移动及插入的更多相关文章

  1. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  2. iOS集合视图单元格高亮和选中的区别

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...

  3. Excel2007VBA数组和工作表及单元格的引用

    动态数组使用: https://zhidao.baidu.com/question/1432222709706721499.html 使用Redim动态数组即可. 1 2 3 4 5 6 7 8 Su ...

  4. iOS中表视图单元格事件用nib和storyboard的两种写法总结

    从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...

  5. IOS开发-表视图LV3导航控制器

    学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...

  6. ABAP 动态内表添加单元格颜色字段

    *动态内表alv显示时要求某些单元格显示颜色 *wa_fldcat-datatype不能添加LVC_T_SCOL类型,在创建好内表之后,再添加颜色列. DATA: wa_fldcat TYPE lvc ...

  7. iOS开发-表视图的使用

    // // ViewController.m // Simple Table // // Created by Jierism on 16/7/20. // Copyright © 2016年 Jie ...

  8. easyui DataGrid表体单元格跨列rowspan

    最近做项目用到了jquery easyui,其中一组DataGrid做的报表是给客户大领导看的,客户要求报表样式跟他们原有系统的一模一样(如下图1). DataGrid样式好调,只是城市名称单元格跨行 ...

  9. 【VBA】单元格插入图片,单元格删除图片

    封装函数: Sub 插入产品形象(strRange As String, datebaseTu As String) Dim strJpg As String strJpg = datebaseTu ...

随机推荐

  1. java中反射

    Person.java===>>person.class ==>>jvm中的类加载器===>>class对象:代表内存中Person.class ==>> ...

  2. 详解YUV与RGB数据格式-2016.01.20

    参考文献 什么是I帧,P帧,B帧 图文详解YUV420数据格式 RGB565 与 RGB888的相互转换 最简单的基于FFmpeg的libswscale的示例(YUV转RGB)

  3. 【WCF】Silverlight+wcf+自定义用户名密码验证

    本文摘自 http://www.cnblogs.com/virusswb/archive/2010/01/26/1656543.html 在昨天的博文Silverlight3+wcf+在不使用证书的情 ...

  4. java值得注意的几个问题

    1.一个源文件中只能有一个类是public的,其他的都是默认权限的: 2.一个类只能作为public或者默认权限(就是没有修饰符的意思): 3.源文件的public类的名字必须要跟文件名保持一致,否则 ...

  5. javaSE第二十三天

    第二十三天    338 1.进程和线程的概述    338 2.多线程(理解)    339 (1)多线程:一个应用程序有多条执行路径    339 (2)Java程序的运行原理及JVM的启动是多线 ...

  6. SQL加、查、改、删、函数

    SQL加.查.改.删.函数   USE lianxiGOcreate table student1(code int not null ,name varchar(20),sex char(4),ci ...

  7. dataGridView 如何默认选中第一行

    datagridview默认选中第一行方法: this.dataGridView1.Rows[0].Selected = true; datagridview 去除 默认选中第一行方法:在绑定data ...

  8. GridView 鼠标经过时变色两种方法

    第一种: 新建一个js文件 并引用 <script src="jquery.js" type="text/javascript"></scri ...

  9. jquery用ajax方式从后台获取json数据,将内容填充到下拉列表。

    从后台获取json数据,将内容填充到下拉列表. url:链接 par:ID sel:下拉列表选择器 //获取下拉列表 function BuildSelectBox(url, par, sel) { ...

  10. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...