设置一个在编辑状态下点击可改变图片的cell

FileItemTableCell.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface FileItemTableCell : UITableViewCell
  4. {
  5. @private
  6. UIImageView* m_checkImageView;
  7. BOOL m_checked;
  8. }
  9.  
  10. - (void) setChecked:(BOOL)checked;
  11.  
  12. @end

FileItemTableCell.m

  1. #import "FileItemTableCell.h"
  2.  
  3. @implementation FileItemTableCell
  4.  
  5. - (void) setCheckImageViewCenter:(CGPoint)pt alpha:(CGFloat)alpha animated:(BOOL)animated
  6. {
  7. if (animated)
  8. {
  9. [UIView beginAnimations:nil context:nil];
  10. [UIView setAnimationBeginsFromCurrentState:YES];
  11. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  12. [UIView setAnimationDuration:0.3];
  13.  
  14. m_checkImageView.center = pt;
  15. m_checkImageView.alpha = alpha;
  16.  
  17. [UIView commitAnimations];
  18. }
  19. else
  20. {
  21. m_checkImageView.center = pt;
  22. m_checkImageView.alpha = alpha;
  23. }
  24. }
  25.  
  26. - (void) setEditing:(BOOL)editting animated:(BOOL)animated
  27. {
  28. if (self.editing == editting)
  29. {
  30. return;
  31. }
  32.  
  33. [super setEditing:editting animated:animated];
  34.  
  35. if (editting)
  36. {
  37. self.selectionStyle = UITableViewCellSelectionStyleNone;
  38.  
  39. // self.backgroundView = [[UIView alloc] init];
  40. // self.backgroundView.backgroundColor = [UIColor whiteColor];
  41. // self.textLabel.backgroundColor = [UIColor clearColor];
  42. // self.detailTextLabel.backgroundColor = [UIColor clearColor];
  43.  
  44. if (m_checkImageView == nil)
  45. {
  46. m_checkImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Unselected.png"]];
  47. [self addSubview:m_checkImageView];
  48. }
  49.  
  50. [self setChecked:m_checked];
  51. m_checkImageView.center = CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
  52. CGRectGetHeight(self.bounds) * 0.5);
  53. m_checkImageView.alpha = 0.0;
  54. [self setCheckImageViewCenter:CGPointMake(20.5, CGRectGetHeight(self.bounds) * 0.5)
  55. alpha:1.0 animated:animated];
  56. }
  57. else
  58. {
  59. m_checked = NO;
  60. // self.selectionStyle = UITableViewCellSelectionStyleBlue;
  61. self.backgroundView = nil;
  62.  
  63. if (m_checkImageView)
  64. {
  65. [self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
  66. CGRectGetHeight(self.bounds) * 0.5)
  67. alpha:0.0
  68. animated:animated];
  69. }
  70. }
  71. }
  72.  
  73. - (void)dealloc
  74. {
  75. m_checkImageView = nil;
  76. }
  77.  
  78. - (void) setChecked:(BOOL)checked
  79. {
  80. if (checked)
  81. {
  82. m_checkImageView.image = [UIImage imageNamed:@"Selected.png"];
  83. self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
  84. }
  85. else
  86. {
  87. m_checkImageView.image = [UIImage imageNamed:@"Unselected.png"];
  88. self.backgroundView.backgroundColor = [UIColor whiteColor];
  89. }
  90. m_checked = checked;
  91. }

ViewController.m

  1. #import "ViewController.h"
  2. #import "FileItemTableCell.h"
  3.  
  4. @interface Item : NSObject
  5.  
  6. @property (retain, nonatomic) NSString *title;
  7.  
  8. @property (assign, nonatomic) BOOL isChecked;
  9.  
  10. @end
  11.  
  12. @implementation Item
  13.  
  14. @end
  15. @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
  16.  
  17. @property (nonatomic,strong)UITableView *tableView;
  18. @property (retain, nonatomic) NSMutableArray *items;
  19. @end
  20.  
  21. @implementation ViewController
  22.  
  23. - (instancetype)init
  24. {
  25. self = [super init];
  26. if (self) {
  27. UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(setEditing:animated:)];
  28. self.navigationItem.rightBarButtonItem = right;
  29.  
  30. UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonPressed)];
  31. self.navigationItem.leftBarButtonItem = left;
  32. }
  33. return self;
  34. }
  35.  
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
  39. self.tableView.rowHeight = ;
  40. self.tableView.allowsSelectionDuringEditing = YES;
  41. self.tableView.dataSource =self;
  42. self.tableView.delegate = self;
  43. [self.view addSubview:self.tableView];
  44. self.items = [NSMutableArray arrayWithCapacity:];
  45. for (int i=; i<; i++) {
  46. Item *item = [[Item alloc] init];
  47. item.title = [NSString stringWithFormat:@"%d",i];
  48. item.isChecked = NO;
  49. [_items addObject:item];
  50. }
  51. }
  52.  
  53. - (void)leftBarButtonPressed {
  54. NSLog(@"删除");
  55. NSMutableArray *array = [[NSMutableArray alloc]initWithArray:_items];
  56. for (int i = ; i < array.count; i ++) {
  57. Item* item = [array objectAtIndex:i];
  58. if (item.isChecked) {
  59. [_items removeObject:item];
  60. }
  61. }
  62. [_tableView reloadData];
  63. NSLog(@"%ld",_items.count);
  64. }
  65.  
  66. - (void) setEditing:(BOOL)editting animated:(BOOL)animated
  67. {
  68. self.navigationItem.rightBarButtonItem.title = _tableView.editing ? @"Edit" : @"Done";
  69. [_tableView setEditing:!_tableView.editing animated:YES];
  70. [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
  71. }
  72.  
  73. #pragma mark -
  74. #pragma mark Table view data source
  75.  
  76. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  77. // Return the number of sections.
  78. return ;
  79. }
  80.  
  81. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  82. {
  83. return [_items count];
  84. }
  85.  
  86. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. return UITableViewCellEditingStyleNone;
  89. }
  90.  
  91. // Customize the appearance of table view cells.
  92. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  93.  
  94. static NSString *CellIdentifier = @"Cell";
  95.  
  96. FileItemTableCell *cell = (FileItemTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  97. if (cell == nil) {
  98. cell = [[FileItemTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
  99. cell.textLabel.font = [cell.textLabel.font fontWithSize:];
  100. }
  101.  
  102. cell.accessoryType = UITableViewCellAccessoryNone;
  103. cell.textLabel.textColor = [UIColor blackColor];
  104.  
  105. Item* item = [_items objectAtIndex:indexPath.row];
  106. cell.textLabel.text = item.title;
  107. [cell setChecked:item.isChecked];
  108. return cell;;
  109.  
  110. }
  111.  
  112. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  113. {
  114. Item* item = [_items objectAtIndex:indexPath.row];
  115.  
  116. if (self.tableView.editing)
  117. {
  118. FileItemTableCell *cell = (FileItemTableCell*)[tableView cellForRowAtIndexPath:indexPath];
  119. item.isChecked = !item.isChecked;
  120. [cell setChecked:item.isChecked];
  121. }
  122. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  123. }
  124.  
  125. @end

使用对象感觉较之前的字典好理解些,也简单些

效果图:

2015.6.4更新

最新效果图:添加全选功能

最新Demo下载地址:http://pan.baidu.com/s/1o6DpN0u

修改了个全选的bug:https://github.com/WuJiForFantasy/UITableViewChooseDelete-.git

UITableView多选删除的更多相关文章

  1. IOS UITableView多选删除功能

    UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车.收藏列表等. 单行删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除 ...

  2. ios UITableView多选删除

    第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ...

  3. UITableView划动删除的实现

    对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个 ...

  4. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  5. cxgrid多选删除

    设置OptionsData选项中的Editing设为True,按着Shift和Ctrl可实现多选 SelectionChanged事件 For i:= 0 To cxGrid1DBTableView1 ...

  6. 【凯子哥带你夯实应用层】使用ActionMode实现有删除动画的多选删除功能

        转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992      ActionMode是3.0之后.官方推荐的一种上下文菜单的实现方式,在之前一直用的是Co ...

  7. GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除)

    GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除) 之前有一个范例,相同的结果可以用两种作法来实践 [GridView] 资料系结表达 ...

  8. [习题] FindControl 简单练习--GridView + CheckBox,点选多列数据(复选删除)#3 List或数组

    [习题] FindControl 简单练习--GridView + CheckBox,点选多列数据(复选删除)#3 List或数组 之前的范例,使用字符串.文字来记录将删除的文章ID 后续会有很多小缺 ...

  9. JQuery Easyui/TopJUI表格基本的删除功能(删除当前行和多选删除)

    需求:数据表格datagrid实现删除当前行和多选删除的功能. html <a href="javascript:void(0)" data-toggle="top ...

随机推荐

  1. 1008. Elevator (20)

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...

  2. 微信支付开发,再次签名,APP调用

    1.商户服务器生成支付订单,先调用[统一下单API]生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付. 再次生成签名的时候,按照接口: https://pay.weixin.q ...

  3. MVC 中的@Html.DropDownList下拉框的使用

    MVC 中的下拉框 实现方式,下面为大家介绍一个我自己认为比较好用的实现方式,而二话不说直接上代码: 第一步: 后台代码 //公共的方法 //在每次需要展示下拉框的时候,需要调用一下这个方法 [数据源 ...

  4. 失败的数据库迁移UDB

    公司采用的是ucloud的云主机,数据库也是架设在云主机上.由于数据越来越多数据查询数据越来越慢,所以我决定往 UDB上迁移.当时考虑的理由如下: (1)云主机底层架设在虚拟机上IO性能有折损,而UD ...

  5. Jackson如何使JSON输出变得优雅?

    本篇文章翻译自:How to enable pretty print JSON output (Jackson) 在这篇文章中,我们将教你如何利用Jackson Library在控制台或者JSP页面优 ...

  6. 在本地环境用虚拟机win2008 sever搭建VS2013 + SVN 代码版本控制环境

    此文仅仅是自己笔记做个备忘.因为自己开发一些中小型的软件经常需要修修改改,特别是winform界面的大改动.经常需要对版本进行管理.而租用分布式服务器和远程服务器都不是自己想要的.本文结合虚拟机 + ...

  7. [uwp开发]数据绑定那些事(1)

    现在是msp候选人,是时候写点技术博客来加分了(实则是个人的心得体会). 注:以下都是个人理解,错误在所难免,欢迎批评指正 以前接触过WPF,只会简单的一些操作,现在在逐渐学习UWP(Universa ...

  8. xml之XSLT

     1.XSLT是什么  XSLT是XSL的子集,XSL是样式表.XSLT的作用:将XML文档转化成HTML,做的是中间转换者. 而主要需要学习的是XSLT(XSLTransformation).  2 ...

  9. “我爱淘”冲刺阶段Scrum站立会议10

    完成任务: 完成了webservice的配置与测试,可以将数据库中的内容解析出来. 计划任务: 在客户端通过查询可以得到想要的书籍内容. 遇到问题: 客户端的内容获取还没有实现.

  10. Android 上传图片到 Asp.Net 服务器的问题

    最近在做一个手机app联合系统管理做的应用程序,管理程序管理数据的发布和增删改查,手机app负责显示和操作业务逻辑这么一个功能. 刚开始路走的都很顺,但是走到通过Android客户端上传图片到Asp. ...