本文共 7983 字,大约阅读时间需要 26 分钟。
- (NSMutableArray *)dataArray { if (nil ==_dataArray) { // 实例化 dataArray#warning 不要忘记实例化 dataArray数组 _dataArray = [NSMutableArrayarray]; // 1. 路径 NSString *path = [[NSBundlemainBundle]pathForResource:@"heros.plist"ofType:nil]; // 2. 读取内容 NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path]; // 3. 可变数组// NSMutableArray *mutable = [NSMutableArray array]; // 4. 字典转模型 for (NSDictionary *dictin tempArray) { HeroModel *model = [HeroModelheroModelWithDict:dict]; [_dataArray addObject:model]; } // _dataArray = mutable; } return_dataArray;}- (void)viewDidLoad { [superviewDidLoad]; // 设置控制器成为tableView的代理 _tableView.delegate =self; // 打开tableView的编辑模式 _tableView.editing = YES; ++++++++++++++++++++++++++++++++// 如果想实现,滑动删除,就必须把这个属性给关闭 _tableView.editing = YES;+++++++++++++++++++++++++++++++++}// 多少组- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}// 多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count;}// 每一行显示的内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // static 避免多次分配内存 static NSString *identifier =@"fdsfdsfds"; // 1. 到缓存池中去找cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // 2. 判断是否取到,如果取不到就实例化新的cell if (nil == cell) { // 实例化tableViewcell cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier]; } // 3. 设置cell上控件的数据 // 设置cell上控件的内容 HeroModel *model = self.dataArray[indexPath.row]; // 设置imageView cell.imageView.image = [UIImageimageNamed:model.icon]; // 设置文本 cell.textLabel.text = model.name; // 设置detailTextLabel cell.detailTextLabel.text = model.intro; // 设置右侧箭头 // accessory : 配件 cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator; return cell;}// 决定那一行是可以被编辑的;如果不实现这个方法默认每一行都可以编辑- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES;}// 点击delete的时候会调用这个方法/** UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 可以对传入的编辑模式进行判断 if (editingStyle ==UITableViewCellEditingStyleDelete) { // 1. 在数据源中把row 对应到 dataArray下标中的对象给移除掉 [self.dataArrayremoveObjectAtIndex:indexPath.row]; // 2. 刷新数据// [_tableView reloadData];// [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [_tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft]; } elseif (editingStyle ==UITableViewCellEditingStyleInsert) { // 1. 根据点击的indexPath.row 在数据源中插入heroModel 对象 HeroModel *model = [[HeroModelalloc]init]; model.name = @"千珏"; [self.dataArrayinsertObject:modelatIndex:indexPath.row]; // 2. 刷新数据 [_tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft ]; } }#pragma mark -#pragma mark - 当一行cell被取消选中的时候会调用- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"调用了--- %ld", indexPath.row);}#pragma mark -#pragma mark - 当cell被选中的时候调用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }#pragma mark -#pragma mark - 决定删除按钮上面显示的文本- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"咔嚓掉";}#pragma mark -#pragma mark - 决定编辑模式,如果不实现者个方法,默认是删除模式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { returnUITableViewCellEditingStyleDelete;}- (BOOL)prefersStatusBarHidden { return YES;}=============cell滑动删除时显示多个按钮======#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ // 添加一个删除按钮 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了删除"); }]; // 删除一个置顶按钮 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了置顶"); }]; topRowAction.backgroundColor = [UIColor blueColor]; // 添加一个更多按钮 UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了更多"); }]; moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; // 将设置好的按钮放到数组中返回 return @[deleteRowAction, topRowAction, moreRowAction];}
***删除cell方法一:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES;}// 定义编辑样式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete;}// 进入编辑模式,按下出现的编辑按钮后,进行删除操作- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { }}// 修改编辑按钮文字- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除";}
*****删除cell方法二:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{// 添加一个删除按钮UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了删除");}]; // 删除一个置顶按钮 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了置顶"); }]; topRowAction.backgroundColor = [UIColor blueColor]; // 添加一个更多按钮 UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"点击了更多");}]; moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; // 将设置好的按钮放到数组中返回 return @[deleteRowAction, topRowAction, moreRowAction]; }
*******删除cell方法三:
//iOS11后的删除方法-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPathAPI_AVAILABLE(ios(11.0)){ UISwipeActionsConfiguration *config; if (@available(iOS 11.0, *)) { UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { }]; deleteRowAction.image = [UIImage imageNamed:@"删除"]; deleteRowAction.backgroundColor = [UIColor redColor]; config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]]; } else { } return config; }
转载地址:http://oqlgi.baihongyu.com/