<个人记录>
左划删除方法:
//ios11.0之前-(NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *sharedAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[tableView setEditing:NO animated:YES];
NSLog(@"分享事件1");
}];
sharedAction.backgroundColor = [UIColor yellowColor];
UITableViewRowAction *delAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[tableView setEditing:NO animated:YES];
NSLog(@"删除事件1");
}];
return @[delAction,sharedAction];
}
//ios11.0
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
if (@available(iOS 11.0, *)) {
UIContextualAction *sharedAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
NSLog(@"分享事件");;
// completionHandler (YES);
}];
UIContextualAction *delAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:nil handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
NSLog(@"删除事件");
// completionHandler (YES);
}];
sharedAction.backgroundColor = [UIColor purpleColor ];
delAction.backgroundColor = [UIColor blueColor];
// sharedAction.image = [UIImage imageNamed:@"sha"];//只满足需求为白色图片
// delAction.image = [UIImage imageNamed:@"del"];//只满足需求为白色图片
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[delAction,sharedAction]];
config.performsFirstActionWithFullSwipe = NO;//ios11.0左划删除划到最右边执行删除事件,默认为YES.此处设置为NO禁止划到最右边执行删除操作。
return config;
} else {
return nil;
}}
项目中左划删除按钮需要展示图片eg:
而iOS11.0之前系统删除按钮只能设置title,不能满足项目需求。需对系统删除按钮自定义,在自定义cell中添加如下方法:
-(void)layoutSubviews{//根据层级获取删除按钮
[super layoutSubviews];
if(@available(iOS 11.0, *))return;
for (UIView *subview in self.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 1)
{
UIButton *deleteButton = subview.subviews[0];
UIButton *sharedBtn = subview.subviews[1];
CGFloat top = (deleteButton.frame.size.height-18)/2.0;
CGFloat left = (deleteButton.frame.size.width-18)/2.0;
[deleteButton setImageEdgeInsets:UIEdgeInsetsMake(top, left, top, left)];
[deleteButton setImage:[UIImage imageNamed:@"del"] forState:UIControlStateNormal];
[sharedBtn setImageEdgeInsets:UIEdgeInsetsMake(top, left, top, left)];
[sharedBtn setImage:[UIImage imageNamed:@"sha"] forState:UIControlStateNormal];
}
}
}
iOS11.0听说删除按钮也支持图片了,想着适配可以直接使用系统方法就完全解决了,没想到愣是把我的图片都给渲染成了白色的图片。。。
黑色的,绿色的 彩色的都只能展示出白色的图片。试了一些方法 也没能把系统的这个只能展示白色图片给解决掉。也是醉了支持设置图片 还把我们设置的图片都给渲染白了。这就只能解决本身是白色图片的需求了。我们的需求是黑色的图片,所以还得自定义这个按钮了。ios11之前删除按钮层级是在cell上的,ios11删除按钮层级有所改变,是在tableview上了。在控制器viewDidLayoutSubviews中添加:
#warning ios 11.0需求为非白色图片时需获取删除按钮,设置图片。白色图片时如下方法直接省掉使用delAction.image = [UIImage imageNamed:@"del"]
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(@available(iOS 11.0, *)){
[self configSwipeButtons];
}}
- (void)configSwipeButtons
{
for (UIView *subview in self.tableView.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 2)
{
UIButton *deleteButton = subview.subviews[1];
UIButton *readButton = subview.subviews[0];
if (deleteButton){
[deleteButton setImage:[UIImage imageNamed:@"del"] forState:UIControlStateNormal];
}
if (readButton){
[readButton setImage:[UIImage imageNamed:@"sha"] forState:UIControlStateNormal];
}}}}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if(@available(iOS 11.0, *)){
[self.view setNeedsLayout]; }
}