uitableview delete button image in iOS

柔情痞子 提交于 2019-12-10 17:56:29

问题


I want to change swipe button image of uitableview cell. I have searched for it but I am not get desired result. I have used this code:

- (void)willTransitionToState:(UITableViewCellStateMask)state{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
                UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
                [deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
                [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];

            }
        }
    }
}

But this is not working in iOS 9. Please suggest me how to achieve this in iOS 9.

If I used this code then its working but image is not set properly:

[[UIButton
      appearanceWhenContainedIn:[TableViewCell class], nil]
     setImage:[UIImage imageNamed:@"Delete-notification.png"] forState:UIControlStateNormal];


-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        NSLog(@"Action to perform with Button 1");
    }];


    return @[button];
}

]1

Thanks in advance!


回答1:


I am using this library https://github.com/CEWendel/SWTableViewCell to display image on swipe button.

#import "SWTableViewCell.h"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        SWTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"idTipsCell"];
        [cell setLeftUtilityButtons:[self leftButtonsWithColor:tipsData.color accessibilityValue:(tipsData.isLiked) ? @"1":@"0"] WithButtonWidth:80.0];
        cell.delegate = self;
        return cell;
  }

- (NSArray *)leftButtonsWithColor:(UIColor*)color accessibilityValue:(NSString*)accessibilityValue {
     NSMutableArray *leftUtilityButtons = [NSMutableArray new];

     [leftUtilityButtons sw_addUtilityButtonWithColor:color icon:[UIImage imageNamed:@"img_like"] accessibilityValue:accessibilityValue];
     [leftUtilityButtons sw_addUtilityButtonWithColor:color icon:[UIImage imageNamed:@"img_share"] accessibilityValue:@"shareButton"];

     return leftUtilityButtons;
 }

Hope it will help you.




回答2:


I wouldn't go for any kind of hack like finding the subview somewhere in the cell's hierarchy. I would just either implement the swiping myself (it's not that hard) or use some pod. See this one:

https://www.cocoacontrols.com/controls/abmenutableviewcell




回答3:


This is not really what you are asking for, but you could replace the "Delete" text with the wastebasket emoji.

func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
    return "🗑"
}


来源:https://stackoverflow.com/questions/34013652/uitableview-delete-button-image-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!