Native定制TableViewCell的删除视图
//找到系统中删除按钮对应的类型
@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....
}
}
}
//设置编辑类型 - 删除
- (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之后,苹果官方增加了UITableVIew的右滑操作接口,即新增了一个代理方法(tableView: editActionsForRowAtIndexPath:)和一个类(UITableViewRowAction),
代理方法返回的是一个数组,我们可以在这个代理方法中定义所需要的操作按钮(删除、置顶等),
这些按钮的类就是UITableViewRowAction。这个类只能定义按钮的显示文字、背景色、和按钮事件。并且返回数组的第一个元素在UITableViewCell的最右侧显示,最后一个元素在最左侧显示。
- (NSArray *)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];
}
可以对自定义按钮添加图片了,然后是如果实现了以下两个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
- ( 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了。见下图对比:
(cell视图的结构可参考文章最开头的截图)
#pragma mark 重写layoutSubview - 自定义删除按钮
- (void)layoutSubviews{
//遍历找到 UITableViewCellDeleteConfirmationView 类的子类的方式,自定义删除按钮的大小,颜色
}
UITableViewCellDeleteConfirmationView这个系统隐藏的视图的出现时机,是出现在侧滑的cell中,作为其subView存在.所以修改按钮样式的代码,是写在自定义的cell.m中.
iOS11之后,提供了新的视图,UISwipeActionPullView,它的等级和自定义cell是平级.
创建一个继承UITableview的类,重写它的layoutSubviews方法,根据图中的结构,遍历出UISwipeActionPullView进行修改
- (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];
}
}
}
}
}
swipeActionPullView.frame = CGRectMake(swipeActionPullViewOX, 10, swipeActionPullViewW, self.cellHeight - 10);
- (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;
}
}
}
}
}