iOS中UITableView的删除按钮图片

3

我想要改变uitableview单元格的滑动按钮图片。我已经搜索过,但没有得到期望的结果。我使用了这段代码:

- (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];

            }
        }
    }
}

但是在iOS 9中无法正常工作。请建议我如何在iOS 9中实现此目标。

如果我使用这段代码,那么它可以工作,但是图片设置不正确:

[[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

提前感谢!


https://dev59.com/vnI-5IYBdhLWcg3w6dC2 - Femina Brahmbhatt
@FeminaBrahmbhatt 我也尝试过这个方法,但是没有起作用 :( - Sudha Tiwari
3个回答

4

我正在使用这个库https://github.com/CEWendel/SWTableViewCell来在滑动按钮上显示图片。

#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;
 }

希望这能对您有所帮助。


0

1
我可以使用UITableViewRowAction来实现这个功能,但我想知道如何在删除按钮上设置图像。 - Sudha Tiwari

-1

这并不是你要的答案,但你可以用垃圾桶表情符号代替“删除”文本。

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

有趣的方法)) - Oleg Gordiichuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接