UITableView滑动删除按钮样式改变

如图[默认样式],当cell中包含间隔区域时,使用系统默认滑动删除样式,会发现删除按钮会占位间隔区。

 

  1. #import "CcMyCarModelsViewController.h"  
  2.   
  3. @interface CcMyCarModelsViewController () <UITableViewDelegate, UITableViewDataSource>  
  4. @property (weak, nonatomic) IBOutlet UITableView *tableView;  
  5.   
  6. @implementation CcMyCarModelsViewController  
  7. #pragma mark - UITableView滑动删除  
  8. // 先要设Cell可编辑  
  9. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {  
  10.     return YES;  
  11. }  
  12.   
  13. // 定义编辑样式  
  14. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {  
  15.     return UITableViewCellEditingStyleDelete;  
  16. }  
  17.   
  18. // 进入编辑模式,按下出现的编辑按钮后,进行删除操作  
  19. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {  
  20.     if (editingStyle == UITableViewCellEditingStyleDelete) {  
  21.         /* 
  22.         [_dataMArr removeObjectAtIndex:indexPath.row]; 
  23.         [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
  24.         [_tableView reloadData]; 
  25.          */  
  26.     }  
  27. }  
  28.   
  29. // 修改编辑按钮文字  
  30. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {  
  31.     return @"删除";  
  32. }  
  33.   
  34. @end  

 

[默认样式]

在自定义cell中覆盖方法:

 

  1. #import "CcMyCarModelsCell.h"  
  2.   
  3. @implementation CcMyCarModelsCell  
  4.   
  5. // 改变滑动删除按钮样式  
  6. - (void)layoutSubviews {  
  7.     [super layoutSubviews];  
  8.     for (UIView *subView in self.subviews){  
  9.         if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {  
  10.             CGRect cRect = subView.frame;  
  11.             cRect.size.height = self.contentView.frame.size.height - 10;  
  12.             subView.frame = cRect;  
  13.               
  14.             UIView *confirmView=(UIView *)[subView.subviews firstObject];  
  15.             // 改背景颜色  
  16.             confirmView.backgroundColor=[UIColor colorWithRed:254/255.0 green:85/255.0 blue:46/255.0 alpha:1];  
  17.             for(UIView *sub in confirmView.subviews){  
  18.                 if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){  
  19.                     UILabel *deleteLabel=(UILabel *)sub;  
  20.                     // 改删除按钮的字体  
  21.                     deleteLabel.font=[UIFont boldSystemFontOfSize:15];  
  22.                     // 改删除按钮的文字  
  23.                     deleteLabel.text=@"删除";  
  24.                 }  
  25.             }  
  26.             break;  
  27.         }  
  28.     }  
  29. }  
  30.   
  31. @end  

 

[改变滑动删除按钮样式]

 
 
posted @ 2017-04-28 17:23  dzldzl  阅读(413)  评论(0)    收藏  举报