IOS-UITableView入门(3)
2023-09-11 14:14:10 时间
UITableView本身自带了(增、删)编辑功能:
1.仅仅要调用UITableView的编辑代码 就会进入编辑状态:
[self.tableView setEditing:!self.tableView.editing animated:YES];
2.进入编辑状态的UITableView会调用代理的
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:以便推断是添加还是删除的方法。
UITableViewCellEditingStyle为一个枚举值,如UITableViewCellEditingStyleDelete,UITableViewCellEditingStyleInsert
总体的代码例如以下:
#pragma mark 点击编辑删除 - (IBAction)trashClick:(id)sender { self.tableView.tag=EDIT_MOVE; [self.tableView setEditing:!self.tableView.editing animated:YES]; } #pragma mark -TableViewDataSource #pragma mark 当出现编辑状态时 假设是删除状态时 点击删除保存时调用的方法 #pragma mark 当为添加状态事 点击添加button保存时调用的方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView.tag==20) { [self.arr removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } else { [self.arr insertObject:@"new row..." atIndex:indexPath.row+1]; NSIndexPath *indexNew=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]; [tableView insertRowsAtIndexPaths:@[indexNew] withRowAnimation:UITableViewRowAnimationRight]; } } #pragma mark 点击编辑时出现的删除或者添加的button - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView.tag==20) { return UITableViewCellEditingStyleDelete; } else { return UITableViewCellEditingStyleInsert; } } #pragma mark 移动item时,以便编辑模式后还能保存编辑的顺序 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSString *currStr=self.arr[sourceIndexPath.row]; [self.arr removeObjectAtIndex:sourceIndexPath.row]; [self.arr insertObject:currStr atIndex:destinationIndexPath.row]; [tableView reloadData]; } #pragma mark 点击编辑添加 - (IBAction)addClick:(id)sender { self.tableView.tag=EDIT_ADD; [self.tableView setEditing:!self.tableView.editing animated:YES]; }
相关文章
- react-native 创建 ios 项目
- iOS 9应用开发教程之使用开关滑块控件以及滚动部署视图
- iOS | 零代码快速集成AGC崩溃服务
- 《iOS 9应用开发入门经典(第7版)》——第1章,第1.2节成为iOS开发人员
- 《iOS 9应用开发入门经典(第7版)》——第1章,第1.3节运行iOS应用
- 《iOS 9应用开发入门经典(第7版)》——第1章,第1.6节小结
- 《iOS 9应用开发入门经典(第7版)》——第2章,第2.1节使用Xcode
- 《iOS 8应用开发入门经典(第6版)》——第1章,第1.3节运行iOS应用
- 《iOS 8应用开发入门经典(第6版)》——第1章,第1.5节进一步探索
- ios UINavigationController
- iOS学习笔记-地图MapKit入门
- iOS 简单引导界面
- ios GUI系统综述:iOS的图形绘制、动画与runloop
- iOS 关于 GIF 图片那点事
- iOS 开发之内购 – AppStore
- iOS 高仿:花田小憩3.0.1
- iOS开发系列--数据存取
- IOS 被拒 关于 iPhone running iOS 10.3.1 on Wi-Fi connected to an IPv6 network.
- TDD的iOS开发初步以及Kiwi使用入门
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- 写给IOS开发工程师的网页前端入门笔记
- iOS之学习资源收集--很好的IOS技术学习网站
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <四> NSMutableArray
- iOS全部150万应用归Win10?微软大计划