iOS UITableView实现滑动删除、增加、多选

本文详细介绍了如何在iOS应用中实现UITableView的滑动删除、动态增加功能以及cell的多选操作。通过实现UITableView的三个代理方法:canEditRowAtIndexPath、editingStyleForRowAtIndexPath和commitEditingStyle:forRowAtIndexPath,可以轻松达成目标。示例代码展示了如何设置编辑状态,并在编辑模式下执行删除和增加操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现滑动删除,动态增加,以及cell的多选只需要添加cell 的三个代理函数。

1.这个函数是判断每一个cell是否可编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

2.这个函数是返回编辑的类型

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

3.这个函数是编辑后的操作。但是多选例外。需要借助selected函数

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath


废话不多说,直接上代码。。清晰明了

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor redColor];

 

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-64) style:UITableViewStylePlain];

_tableView.delegate = self;

_tableView.dataSource = self;

[self.view addSubview:_tableView];

 

 

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(rightItemEvents:)];

self.navigationItem.rightBarButtonItem = rightItem;

}


-(void)rightItemEvents:(UIBarButtonItem *)item

{

_tableView.editing = !_tableView.editing;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"选中:%d%d",indexPath.section,indexPath.row);

}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

//在这里做你想做的

if (editingStyle == UITableViewCellEditingStyleDelete) {

NSLog(@"删除");

}else if (editingStyle == UITableViewCellEditingStyleInsert){

NSLog(@"增加");

}else{

NSLog(@":%d",indexPath.section);

}

 

}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

//是否允许编辑

return YES;

}


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

//设置编辑的类型

 

return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 5;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 5;

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值