[iOS]UITableView滑动删除按钮样式改变
2016-11-29 10:29
549 查看
[iOS]UITableView滑动删除按钮样式改变
本文产生因由:
如图[默认样式],当cell中包含间隔区域时,使用系统默认滑动删除样式,会发现删除按钮会占位间隔区。
#import "CcMyCarModelsViewController.h"
@interface CcMyCarModelsViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@implementation CcMyCarModelsViewController
#pragma mark - UITableView滑动删除
// 先要设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) {
/*
[_dataMArr removeObjectAtIndex:indexPath.row];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[_tableView reloadData];
*/
}
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
@end
[默认样式]
在自定义cell中覆盖方法:
#import "CcMyCarModelsCell.h"
@implementation CcMyCarModelsCell
// 改变滑动删除按钮样式
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews){
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
CGRect cRect = subView.frame;
cRect.size.height = self.contentView.frame.size.height - 10;
subView.frame = cRect;
UIView *confirmView=(UIView *)[subView.subviews firstObject];
// 改背景颜色
confirmView.backgroundColor=[UIColor colorWithRed:254/255.0 green:85/255.0 blue:46/255.0 alpha:1];
for(UIView *sub in confirmView.subviews){
if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){
UILabel *deleteLabel=(UILabel *)sub;
// 改删除按钮的字体
deleteLabel.font=[UIFont boldSystemFontOfSize:15];
// 改删除按钮的文字
deleteLabel.text=@"删除";
}
}
break;
}
}
}
@end
[改变滑动删除按钮样式]
本文产生因由:
如图[默认样式],当cell中包含间隔区域时,使用系统默认滑动删除样式,会发现删除按钮会占位间隔区。
#import "CcMyCarModelsViewController.h"
@interface CcMyCarModelsViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@implementation CcMyCarModelsViewController
#pragma mark - UITableView滑动删除
// 先要设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) {
/*
[_dataMArr removeObjectAtIndex:indexPath.row];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[_tableView reloadData];
*/
}
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
@end
[默认样式]
在自定义cell中覆盖方法:
#import "CcMyCarModelsCell.h"
@implementation CcMyCarModelsCell
// 改变滑动删除按钮样式
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subView in self.subviews){
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
CGRect cRect = subView.frame;
cRect.size.height = self.contentView.frame.size.height - 10;
subView.frame = cRect;
UIView *confirmView=(UIView *)[subView.subviews firstObject];
// 改背景颜色
confirmView.backgroundColor=[UIColor colorWithRed:254/255.0 green:85/255.0 blue:46/255.0 alpha:1];
for(UIView *sub in confirmView.subviews){
if([sub isKindOfClass:NSClassFromString(@"UIButtonLabel")]){
UILabel *deleteLabel=(UILabel *)sub;
// 改删除按钮的字体
deleteLabel.font=[UIFont boldSystemFontOfSize:15];
// 改删除按钮的文字
deleteLabel.text=@"删除";
}
}
break;
}
}
}
@end
[改变滑动删除按钮样式]
相关文章推荐
- UITableView滑动删除按钮样式改变
- iOS开发学习之利用系统远程UITableView 自定义滑动删除按钮、增加自定义滑动按钮方法
- iOS中uitableview滑动删除添加按钮
- IOS 开发使用UITableView 实现滑动 删除等多个按钮
- (iOS)UITableView只允许部分cell支持滑动删除
- iOS UITableView 支持滑动删除 Cell
- ios UITableView实现单击提示,滑动删除,多选行等操作
- [IOS]UITableView实现Cell的滑动删除
- IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能
- ios 中 关闭uitableview 中手势滑动出现按钮的功能
- ios 如何让TableView加入横向滑动出现删除按钮的功能
- iOS UITableView(十三) tableView经常用的代理总结和滑动删除
- UITableView 左右滑动时,出现删除按钮的动画
- UITableView 左右滑动时,出现删除按钮的动画(可自定义)。
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
- ios-day13-01(私人通讯录。控制器之间的数据传递、保存密码和自动登录的实现、普通对象的归档、UITableView的局部刷新和向左滑动删除功能等等)
- iOS 开发 UItableView中滑动删除 cell
- iOS中给UITableView的侧滑删除增加多个按钮的实现方法
- UITableView左右滑动cell无法显示“删除”按钮的原因分析
- iOS UITableView实现滑动删除、增加、多选