<個(gè)人記錄>
左劃刪除方法:
//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左劃刪除劃到最右邊執(zhí)行刪除事件,默認(rèn)為YES.此處設(shè)置為NO禁止劃到最右邊執(zhí)行刪除操作。
return config;
} else {
return nil;
}}
項(xiàng)目中左劃刪除按鈕需要展示圖片eg:
而iOS11.0之前系統(tǒng)刪除按鈕只能設(shè)置title,不能滿足項(xiàng)目需求合冀。需對(duì)系統(tǒng)刪除按鈕自定義赡模,在自定義cell中添加如下方法:
-(void)layoutSubviews{//根據(jù)層級(jí)獲取刪除按鈕
??? [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聽說刪除按鈕也支持圖片了加缘,想著適配可以直接使用系統(tǒng)方法就完全解決了锤悄,沒想到愣是把我的圖片都給渲染成了白色的圖片衣形。辣往。兔院。
黑色的,綠色的 彩色的都只能展示出白色的圖片站削。試了一些方法 也沒能把系統(tǒng)的這個(gè)只能展示白色圖片給解決掉坊萝。也是醉了支持設(shè)置圖片 還把我們?cè)O(shè)置的圖片都給渲染白了。這就只能解決本身是白色圖片的需求了许起。我們的需求是黑色的圖片十偶,所以還得自定義這個(gè)按鈕了。ios11之前刪除按鈕層級(jí)是在cell上的街氢,ios11刪除按鈕層級(jí)有所改變扯键,是在tableview上了。在控制器viewDidLayoutSubviews中添加:
#warning ios 11.0需求為非白色圖片時(shí)需獲取刪除按鈕珊肃,設(shè)置圖片荣刑。白色圖片時(shí)如下方法直接省掉使用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]; }
}