单元格的多选删除
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property(retain)UITableView* tableView;@property(retain)NSMutableArray* dataSource;@property(retain)NSMutableArray* removeData;@property(assign)BOOL isEdit;
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super viewDidLoad
];
self.view.backgroundColor=[UIColor cyanColor
];
//防止下滑
self.automaticallyAdjustsScrollViewInsets=NO
;
//申请内存
_dataSource=[NSMutableArray new
];
_removeData=[NSMutableArray new
];
//创建表格视图
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
//添加
//NSMutableArray* arr=[NSMutableArray new];
for (int j=0; j<15
; j++) {
NSString* str=[NSString stringWithFormat:@"男%d号"
,j];
//[arr addObject:str];
[
_dataSource addObject
:str];
}
//显示表格的编辑按钮
self.navigationItem.rightBarButtonItem=self.editButtonItem
;
//表格视图代理
_tableView.delegate=self
;
//数据源代理
_tableView.dataSource=self
;
[
self.view addSubview:_tableView
];
}
//实现控制表格的编辑状态开启或者关闭的方法
-(
void)setEditing:(BOOL)editing animated:(BOOL
)animated
{
[
super setEditing:editing animated
:animated];
//取非
_isEdit=!_isEdit
;
//开启表格的标记状态
[
_tableView setEditing:_isEdit animated:YES
];
}
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
;
}
-(
UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//
static NSString* str=@"cell"
;
NSLog(@"单元格"
);
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier
:str];
//如果不存在就创建
if (cell==nil
) {
//创建单元格
cell=[[
UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier
:str];
}
//设置标题 因为标题在数据源中的小数组里面,所以必须先取到小数组 才能取到真正的标题内容 小数组的获取可以通过区的索引值来获取 标题可以通过行的索引值获取 也就是indexPath indexPath里面包含了当前组的索引值和当前行的索引值 indexPath.section 是当前组的索引值 indexPath.row 当前行的索引值
cell.
textLabel.text=_dataSource[indexPath.row
];
return
cell;
}
-(
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
)section
{
NSLog(@"行"
);
return [_dataSource count
];
}
//返回每个区尾部视图的高度
-(
CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger
)section
{
return 30
;
}
-(
UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger
)section
{
UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom
];
btn.
frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30
);
btn.
backgroundColor=[UIColor cyanColor
];
[btn
addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside
];
[btn
setTitle:@"Delete" forState:UIControlStateNormal
];
return
btn;
}
-(
void)btnAction:(UIButton
*)sender
{
NSLog(@" +++++"
);
//如果进入编辑状态才可以删除
if (_isEdit==YES
) {
//吧数据源内_removeData里的数据全部清空
[
_dataSource removeObjectsInArray:_removeData
];
//清空上次选中行的数据
[
_removeData removeAllObjects
];
//当表格的数据源发生变化时 reloadData 向表格发送消息 可以重新加载表格视图
[
_tableView reloadData
];
}
}
-(
void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath
*)indexPath
{
//先获取被选中行的数据
NSString* obj=[_dataSource objectAtIndex:indexPath.row
];
//存到 数组中
[
_removeData addObject
:obj];
}
//当用户又一次点击某一行 则之前的行会调用这个方法(反选行操作)
-(
void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
*)indexPath
{
//参数 indexPath 存放的就是上次被选中的行的信息
//移除上次选中该行进行 _removeData 中的信息
NSString* str=[_dataSource objectAtIndex:indexPath.row
];
if ([_removeData containsObject
:str]) {
[
_removeData removeObject
:str];
}
}
- (
void
)didReceiveMemoryWarning {
[
super didReceiveMemoryWarning
];
// Dispose of any resources that can be recreated.
}
@end
---恢复内容结束---
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property(retain)UITableView* tableView;@property(retain)NSMutableArray* dataSource;@property(retain)NSMutableArray* removeData;@property(assign)BOOL isEdit;
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super viewDidLoad
];
self.view.backgroundColor=[UIColor cyanColor
];
//防止下滑
self.automaticallyAdjustsScrollViewInsets=NO
;
//申请内存
_dataSource=[NSMutableArray new
];
_removeData=[NSMutableArray new
];
//创建表格视图
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
//添加
//NSMutableArray* arr=[NSMutableArray new];
for (int j=0; j<15
; j++) {
NSString* str=[NSString stringWithFormat:@"男%d号"
,j];
//[arr addObject:str];
[
_dataSource addObject
:str];
}
//显示表格的编辑按钮
self.navigationItem.rightBarButtonItem=self.editButtonItem
;
//表格视图代理
_tableView.delegate=self
;
//数据源代理
_tableView.dataSource=self
;
[
self.view addSubview:_tableView
];
}
//实现控制表格的编辑状态开启或者关闭的方法
-(
void)setEditing:(BOOL)editing animated:(BOOL
)animated
{
[
super setEditing:editing animated
:animated];
//取非
_isEdit=!_isEdit
;
//开启表格的标记状态
[
_tableView setEditing:_isEdit animated:YES
];
}
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
;
}
-(
UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//
static NSString* str=@"cell"
;
NSLog(@"单元格"
);
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier
:str];
//如果不存在就创建
if (cell==nil
) {
//创建单元格
cell=[[
UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier
:str];
}
//设置标题 因为标题在数据源中的小数组里面,所以必须先取到小数组 才能取到真正的标题内容 小数组的获取可以通过区的索引值来获取 标题可以通过行的索引值获取 也就是indexPath indexPath里面包含了当前组的索引值和当前行的索引值 indexPath.section 是当前组的索引值 indexPath.row 当前行的索引值
cell.
textLabel.text=_dataSource[indexPath.row
];
return
cell;
}
-(
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
)section
{
NSLog(@"行"
);
return [_dataSource count
];
}
//返回每个区尾部视图的高度
-(
CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger
)section
{
return 30
;
}
-(
UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger
)section
{
UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom
];
btn.
frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30
);
btn.
backgroundColor=[UIColor cyanColor
];
[btn
addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside
];
[btn
setTitle:@"Delete" forState:UIControlStateNormal
];
return
btn;
}
-(
void)btnAction:(UIButton
*)sender
{
NSLog(@" +++++"
);
//如果进入编辑状态才可以删除
if (_isEdit==YES
) {
//吧数据源内_removeData里的数据全部清空
[
_dataSource removeObjectsInArray:_removeData
];
//清空上次选中行的数据
[
_removeData removeAllObjects
];
//当表格的数据源发生变化时 reloadData 向表格发送消息 可以重新加载表格视图
[
_tableView reloadData
];
}
}
-(
void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath
*)indexPath
{
//先获取被选中行的数据
NSString* obj=[_dataSource objectAtIndex:indexPath.row
];
//存到 数组中
[
_removeData addObject
:obj];
}
//当用户又一次点击某一行 则之前的行会调用这个方法(反选行操作)
-(
void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
*)indexPath
{
//参数 indexPath 存放的就是上次被选中的行的信息
//移除上次选中该行进行 _removeData 中的信息
NSString* str=[_dataSource objectAtIndex:indexPath.row
];
if ([_removeData containsObject
:str]) {
[
_removeData removeObject
:str];
}
}
- (
void
)didReceiveMemoryWarning {
[
super didReceiveMemoryWarning
];
// Dispose of any resources that can be recreated.
}
@end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端