iOS11之前,自定义删除按钮
//
//找到系统中删除按钮对应的类型
UITableViewCellDeleteConfirmationView
去自定义的cell里面 找到并修改此view样式
@implementation SCTableViewCell
- (void)layoutSubviews
{
[super layoutSubviews];
UIView *view = nil;
for (UIView *v in self.subviews)
{
if ([v isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])
{
view = v;
break;
}
}
// View=nil, subviews=nil, count=0都是false
if (view.subviews.count)
{
// 安全拿到Button
UIButton *button = view.subviews[0];
if (button)
{
// do sth....
}
}
}
============重新整理归纳========
iOS8之前(之后也适用),开启tableview的删除功能需要
//设置编辑类型 - 删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
//设置删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除我吧!";
}
//开启侧滑编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
//做删除处理
}
在iOS8之后,提供了侧滑按钮的文字+背景色+事件处理的自定义接口
在iOS8之后,苹果官方增加了UITableVIew的右滑操作接口,即新增了一个代理方法(tableView: editActionsForRowAtIndexPath:)和一个类(UITableViewRowAction),
代理方法返回的是一个数组,我们可以在这个代理方法中定义所需要的操作按钮(删除、置顶等),
这些按钮的类就是UITableViewRowAction。这个类只能定义按钮的显示文字、背景色、和按钮事件。并且返回数组的第一个元素在UITableViewCell的最右侧显示,最后一个元素在最左侧显示。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
-
UITableViewRowAction *rowActionOne = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"点我删删删!" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"0-0-0-0-");
}];
UITableViewRowAction *rowActionTwo = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"点我添添添!" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"0-0-0-0-");
}];
return @[rowActionOne,rowActionTwo];
}
iOS11之后,多了自定义按钮图片功能,且类型为侧滑后一直向右滑动,可至极触发 - (void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{…}方法
可以对自定义按钮添加图片了,然后是如果实现了以下两个iOS 11新增的代理方法,将会取代(tableView: editActionsForRowAtIndexPath:)代理方法:
// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
iOS11下用法
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
//删除
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler (YES);
}];
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
deleteRowAction.backgroundColor = [UIColor blueColor];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
}
创建UIContextualAction对象时,UIContextualActionStyle有两种类型,如果是置顶、已读等按钮就使用UIContextualActionStyleNormal类型,delete操作按钮可使用UIContextualActionStyleDestructive类型,当使用该类型时,如果是右滑操作,一直向右滑动某个cell,会直接执行删除操作,不用再点击删除按钮,这也是一个好玩的更新。
滑动操作这里还有一个需要注意的是,当cell高度较小时,会只显示image,不显示title,当cell高度够大时,会同时显示image和title。我写demo测试的时候,因为每个cell的高度都较小,所以只显示image,然后我增加cell的高度后,就可以同时显示image和title了。见下图对比:
以上iOS11部分,转载自 你可能需要为你的 APP 适配 iOS 11
+++++++ 再来看我的问题 +++++++
基本上iOS11的接口已经足够满足我们自定义删除按钮,而且功能也很酷炫.
但是,这些自定义的按钮的高度,都是贴合整个Cell 的高度,因此想要调整高度的话,通过iOS8或iOS11的新接口,其实都不能满足需求,再看最开始介绍的方法
iOS11之前,我们通过重写
(cell视图的结构可参考文章最开头的截图)
#pragma mark 重写layoutSubview - 自定义删除按钮
- (void)layoutSubviews{
//遍历找到 UITableViewCellDeleteConfirmationView 类的子类的方式,自定义删除按钮的大小,颜色
}
iOS11之后, 这个方法不适用,看下图的对比
iOS11之前
UITableViewCellDeleteConfirmationView这个系统隐藏的视图的出现时机,是出现在侧滑的cell中,作为其subView存在.所以修改按钮样式的代码,是写在自定义的cell.m中.
iOS11之后
iOS11之后,提供了新的视图,UISwipeActionPullView,它的等级和自定义cell是平级.
所以,在iOS11之后,自定义的代码可以写在UITableview中,
创建一个继承UITableview的类,重写它的layoutSubviews方法,根据图中的结构,遍历出UISwipeActionPullView进行修改
iOS11以后适配代码
- (void)layoutSubviews{
[super layoutSubviews];
//iOS11版本以上,自定义删除按钮高度方法:
if (IOS_VERSION_11) {
for (UIView *subview in self.subviews)
{
//1.修改背景
if([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
{
UIView *swipeActionPullView = subview;
//修改背景高度
CGFloat swipeActionPullViewOX = swipeActionPullView.frame.origin.x;
CGFloat swipeActionPullViewW = swipeActionPullView.frame.size.width;
swipeActionPullView.frame = CGRectMake(swipeActionPullViewOX, 10, swipeActionPullViewW, self.cellHeight - 10);
//修改背景颜色
swipeActionPullView.backgroundColor = [UIColor colorWithRed:255/255.f green:173/255.f blue:69/255.f alpha:1.0f];
//修改背景圆角
swipeActionPullView.layer.cornerRadius = 5.f;
swipeActionPullView.layer.masksToBounds = YES;
//2.修改按钮-颜色
UIButton *swipeActionStandardBtn = subview.subviews[0];
if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
swipeActionStandardBtn.backgroundColor = [UIColor colorWithRed:255/255.f green:173/255.f blue:69/255.f alpha:1.0f];
}
}
}
}
}
问题到这里算是解决, ——- 了吗? :-D
由于每个cell与UISwipeActionPullView都是平级关系而不是从属关系
UISwipeActionPullView在侧滑时,才去创建,而且UISwipeActionPullView的frame是根据每个cell的frame去创建的,所以以上写法中的
swipeActionPullView.frame = CGRectMake(swipeActionPullViewOX, 10, swipeActionPullViewW, self.cellHeight - 10);
会有有问题
这样会倒是侧滑cell总是显示在第一个cell上,因为我把Y点的值固定写死了~
现在调整如下,UISwipeActionPullView的背景色设置透明,改为修改UISwipeActionPullView的子视图中的按钮的frame.
- (void)layoutSubviews{
[super layoutSubviews];
//iOS11版本以上,自定义删除按钮高度方法:
if (IOS_VERSION_11) {
for (UIView *subview in self.subviews)
{
if([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
{
UIView *swipeActionPullView = subview;
//1.0修改背景颜色
swipeActionPullView.backgroundColor = [UIColor clearColor];
//1.1修改背景圆角
swipeActionPullView.layer.cornerRadius = 5.f;
swipeActionPullView.layer.masksToBounds = YES;
//2.0修改按钮-颜色
UIButton *swipeActionStandardBtn = subview.subviews[0];
if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
CGFloat swipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
CGFloat swipeActionStandardBtnW = swipeActionStandardBtn.frame.size.width;
swipeActionStandardBtn.frame = CGRectMake(swipeActionStandardBtnOX, 10, swipeActionStandardBtnW, self.cellHeight - 10);
//2.1修改按钮背景色
swipeActionStandardBtn.backgroundColor = [UIColor colorWithRed:255/255.f green:173/255.f blue:69/255.f alpha:1.0f];
//2.2修改按钮背景圆角
swipeActionStandardBtn.layer.cornerRadius = 5.f;
swipeActionStandardBtn.layer.masksToBounds = YES;
}
}
}
}
}