设置一个在编辑状态下点击可改变图片的cell
FileItemTableCell.h
#import@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 ()@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 = 50; self.tableView.allowsSelectionDuringEditing = YES; self.tableView.dataSource =self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; self.items = [NSMutableArray arrayWithCapacity:0]; for (int i=0; i<50; 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 = 0; 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 1;}- (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:17]; } 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下载地址:
修改了个全选的bug: