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

FileItemTableCell.h

#import <UIKit/UIKit.h>

@interface FileItemTableCell : UITableViewCell
{
@private
UIImageView* m_checkImageView;
BOOL m_checked;
} - (void) setChecked:(BOOL)checked; @end

FileItemTableCell.m

#import "FileItemTableCell.h"

@implementation FileItemTableCell

- (void) setCheckImageViewCenter:(CGPoint)pt alpha:(CGFloat)alpha animated:(BOOL)animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3]; m_checkImageView.center = pt;
m_checkImageView.alpha = alpha; [UIView commitAnimations];
}
else
{
m_checkImageView.center = pt;
m_checkImageView.alpha = alpha;
}
} - (void) setEditing:(BOOL)editting animated:(BOOL)animated
{
if (self.editing == editting)
{
return;
} [super setEditing:editting animated:animated]; if (editting)
{
self.selectionStyle = UITableViewCellSelectionStyleNone; // self.backgroundView = [[UIView alloc] init];
// self.backgroundView.backgroundColor = [UIColor whiteColor];
// self.textLabel.backgroundColor = [UIColor clearColor];
// self.detailTextLabel.backgroundColor = [UIColor clearColor]; if (m_checkImageView == nil)
{
m_checkImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Unselected.png"]];
[self addSubview:m_checkImageView];
} [self setChecked:m_checked];
m_checkImageView.center = CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
CGRectGetHeight(self.bounds) * 0.5);
m_checkImageView.alpha = 0.0;
[self setCheckImageViewCenter:CGPointMake(20.5, CGRectGetHeight(self.bounds) * 0.5)
alpha:1.0 animated:animated];
}
else
{
m_checked = NO;
// self.selectionStyle = UITableViewCellSelectionStyleBlue;
self.backgroundView = nil; if (m_checkImageView)
{
[self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
CGRectGetHeight(self.bounds) * 0.5)
alpha:0.0
animated:animated];
}
}
} - (void)dealloc
{
m_checkImageView = nil;
} - (void) setChecked:(BOOL)checked
{
if (checked)
{
m_checkImageView.image = [UIImage imageNamed:@"Selected.png"];
self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
}
else
{
m_checkImageView.image = [UIImage imageNamed:@"Unselected.png"];
self.backgroundView.backgroundColor = [UIColor whiteColor];
}
m_checked = checked;
}

ViewController.m

#import "ViewController.h"
#import "FileItemTableCell.h" @interface Item : NSObject @property (retain, nonatomic) NSString *title; @property (assign, nonatomic) BOOL isChecked; @end @implementation Item @end
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,strong)UITableView *tableView;
@property (retain, nonatomic) NSMutableArray *items;
@end @implementation ViewController - (instancetype)init
{
self = [super init];
if (self) {
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(setEditing:animated:)];
self.navigationItem.rightBarButtonItem = right; UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonPressed)];
self.navigationItem.leftBarButtonItem = left;
}
return self;
} - (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
self.tableView.rowHeight = ;
self.tableView.allowsSelectionDuringEditing = YES;
self.tableView.dataSource =self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
self.items = [NSMutableArray arrayWithCapacity:];
for (int i=; i<; i++) {
Item *item = [[Item alloc] init];
item.title = [NSString stringWithFormat:@"%d",i];
item.isChecked = NO;
[_items addObject:item];
}
} - (void)leftBarButtonPressed {
NSLog(@"删除");
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:_items];
for (int i = ; i < array.count; i ++) {
Item* item = [array objectAtIndex:i];
if (item.isChecked) {
[_items removeObject:item];
}
}
[_tableView reloadData];
NSLog(@"%ld",_items.count);
} - (void) setEditing:(BOOL)editting animated:(BOOL)animated
{
self.navigationItem.rightBarButtonItem.title = _tableView.editing ? @"Edit" : @"Done";
[_tableView setEditing:!_tableView.editing animated:YES];
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
} #pragma mark -
#pragma mark Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_items count];
} - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
} // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; FileItemTableCell *cell = (FileItemTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FileItemTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.textLabel.font = [cell.textLabel.font fontWithSize:];
} cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.textColor = [UIColor blackColor]; Item* item = [_items objectAtIndex:indexPath.row];
cell.textLabel.text = item.title;
[cell setChecked:item.isChecked];
return cell;; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Item* item = [_items objectAtIndex:indexPath.row]; if (self.tableView.editing)
{
FileItemTableCell *cell = (FileItemTableCell*)[tableView cellForRowAtIndexPath:indexPath];
item.isChecked = !item.isChecked;
[cell setChecked:item.isChecked];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} @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. 6.24 AppCan移动开发者大会,我爱我家即将闪亮登场!

    6.24 AppCan移动开发者大会进入倒计时,报名通道即将关闭! “6月24日, 2016AppCan移动开发者大会即将召开,以“平台之上,应用无限”为主题,1500位行业精英汇聚在此,重磅新品发布 ...

  2. 史上最佳 Mac+PhpStorm+XAMPP+Xdebug 集成开发和断点调试环境的配置

    在上一篇 PHP 系列的文章<PHP 集成开发环境比较>中,我根据自己的亲身体验,非常简略的介绍和对比了几款常用的集成开发环境,就我个人而言,比较推崇 Zend Studio 和 PhpS ...

  3. 通过firefox+ProxySelector+dtunnel_lite实现代理上网

    通过firefox+ProxySelector+dtunnel_lite实现代理上网 dtunnel_lite:http://dog-tunnel.tk/下载lite版本就可以 远端:./dtunne ...

  4. 27.some company's Spi Flash chip replace altera epcsxxx

    由于altera公司的epcsxxx芯片比较贵,所以一般用其它公司的spi flash芯片代替也可以.据AlteraFAE描述:“EPCS器件也是选用某家公司的SPIFlash,只是中间经过Alter ...

  5. android不要标题栏

    去除标题栏title其实非常简单,他有两种方法,一种是在代码中添加,另一种是在AndroidManifest.xml中添加: 1.在代码中实现: 在此方法setContentView(R.layout ...

  6. Ubuntu 14.04安装配置NFS服务器

    (一)安装NFS服务器1.1-安装Ubuntu nfs服务器端: sudo apt-get install nfs-kernel-server 1.2-安装nfs的客户端: sudo apt-get ...

  7. java 顺序表

    想看看java版的数据结构,了解一下树的一些操作,写了个顺序表熟悉一下 package com.sqlist; /** * @author xiangfei * 定义一个顺序表 * */ public ...

  8. 软件工程课后作业——用JAVA编写的随机产生30道四则运算

    package com.java.sizeyunsuan; public class lianxi { String f() { int i=(int)(Math.random()*10); int ...

  9. php大文件下载

    <?php header("Content-Type:text/html;charset:utf-8"); //class FileDownServer{ //$file_n ...

  10. 深入探讨Java类加载机制

    一.前言 毕业至今,已经三年光景,平时基本接触不到关于类加载器的技术(工作上),相信很多同行在开始工作后很长一段时间,对于类的加载机制都没有深入的了解过,之前偶然的机会接触了相关的知识,感觉挺有意思, ...