iOS TabelViewCell 删除 编辑 插入
2024-10-15 20:03:06
/** TableView 进入或退出编辑状态(TableView 方法). */
- (void)setEditing:(BOOL)editing animated:(BOOL)animate{
/*首先调用父类的方法*/
[super setEditing:editing animated:animated];
/*使tableView出于编辑状态*/
[self.tableView setEditing:editing animated:animated];
}
/** 确定哪些行的cell可以编辑 (UITableViewDataSource协议中方法). */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (0 == indexPath.row) {
return No; /*第一行不能进行编辑*/
} else {
return Yes;
}
}
/** 设置某一行cell的编辑模式 (UITableViewDelegate协议中方法). */
- (TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row > 10){
return UITableViewCellEditingStyleInsert;
}else{
return
UITableViewCellEditingStyleDelete;
}
}
/** 提交编辑状态 (UITableViewDataSource协议中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
/** 点击 删除 按钮的操作 */
if (editingStyle == UITableViewCellEditingStyleDelete) {
/**< 判断编辑状态是删除时. */ /** 1. 更新数据源(数组): 根据indexPaht.row作为数组下标, 从数组中删除数据. */
[self.arr removeObjectAtIndex:indexPath.row];
/** 2. TableView中 删除一个cell. */
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
/** 点击 +号 图标的操作. */
if (editingStyle == UITableViewCellEditingStyleInsert) {
/**< 判断编辑状态是插入时. */ /** 1. 更新数据源:向数组中添加数据. */
[self.arr insertObject:@"abcd" atIndex:indexPath.row]; /** 2. TableView中插入一个cell. */
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
/** 提交编辑状态 (UITableViewDataSource协议中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
/** 插入 cell (UITableView 方法). */
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
/** 删除 cell (UITableView 方法). */
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
上面的需求思路:
1.tableView 要进入可编辑状态(根据自己的需要)
2.更具自己需要,删除,或者,插入,指定Cell
3.最后对数据进行操作,刷新页面
最新文章
- Delphi_02_Delphi程序的结构
- day4总结
- JSPatch常见问题解答
- UITableView 使用
- Laravel学习
- C# 7.0 新特性4: 返回引用
- ubuntu 设置静态ip
- Spring源码:IOC原理解析(一)
- IPV4/IPV6网络地址相关知识
- 【笔记】【VSCode】Windows下VSCode编译调试c/c++
- win10 uwp 修改Pivot Header 颜色
- Kotlin——从无到有系列之高级篇(一):Lambda表达式
- js获取当前时间并实时刷新
- Xamarin常见问题
- python中的os.listdir()函数
- java post json sample
- FortiGate日志设置
- 题解——loj6280 数列分块入门4 (分块)
- 学JS的心路历程-Promise(一)
- jpa-入门测试