博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITableView多选删除
阅读量:6540 次
发布时间:2019-06-24

本文共 6934 字,大约阅读时间需要 23 分钟。

设置一个在编辑状态下点击可改变图片的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:

转载于:https://www.cnblogs.com/hxwj/p/4536499.html

你可能感兴趣的文章
Loj #6073.「2017 山东一轮集训 Day5」距离
查看>>
我的TCP/IP学习笔记
查看>>
轮毂电机光电增量编码器的ABZ信号详解
查看>>
TextBox Template
查看>>
Linux MySQL 储存中文失败简单解决办法
查看>>
洛谷——P1330 封锁阳光大学
查看>>
css选择器
查看>>
linux系统配置之bash shell的配置(centos)
查看>>
linux C 9*9
查看>>
python的string操作总结
查看>>
如何把word中的图片怎么导出来呢?
查看>>
CMD指令大全
查看>>
Qt多线程学习:创建多线程
查看>>
设计模式学习---UML常见关系的实现
查看>>
图解openssl实现私有CA
查看>>
BZOJ2213 : [Poi2011]Difference
查看>>
c++ Constructor FAQ 继续
查看>>
事务之六:spring 嵌套事务
查看>>
C#:路径
查看>>
iOS图片加载速度极限优化—FastImageCache解析
查看>>