【问题标题】:uitableview delete button image in iOSiOS中的uitableview删除按钮图像
【发布时间】:2015-12-01 06:03:04
【问题描述】:

我想更改 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

提前致谢!

【问题讨论】:

标签: ios objective-c uitableview ios9.1


【解决方案1】:

我正在使用这个库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;
 }

希望对你有帮助。

【讨论】:

    【解决方案2】:

    我不会进行任何类型的黑客攻击,例如在单元格的层次结构中的某处查找子视图。我要么自己实现刷卡(这并不难),要么使用一些吊舱。看到这个:

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

    【讨论】:

    • 我可以为此使用 UITableViewRowAction 但我想知道如何在删除按钮上设置图像。
    【解决方案3】:

    这并不是您真正想要的,但您可以将“删除”文本替换为废纸篓表情符号。

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

    【讨论】:

    • 有趣的方法))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多