【问题标题】:UIButton swizzling causing issue with UITableViewCellAccessoryType'sUIButton swizzling 导致 UITableViewCellAccessoryType 出现问题
【发布时间】:2012-04-13 03:15:33
【问题描述】:

buttonTypeUIButtonTypeCustom 为真时,我有一个类会混淆UIButton。 然而,当涉及到UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryDisclosure 时也是如此。正在发生的事情是,出于某种原因,它正在调整它们并在 accessoryType 后面添加自定义背景等。

我需要做的是检查我试图调配的UIButton 是否是UITableViewCellAccessoryType,但我不知道如何做这样的事情。

这是我用来调配UIButton 的函数的内部结构。

if ([self isMemberOfClass:[UIButton class]] && self.buttonType == UIButtonTypeCustom) {

        UIImage *upImage = [theme rectButtonUp], *downImage = [theme rectButtonDown];
        UIColor *upColor = [theme rectButtonUpTextColor], *downColor = [theme rectButtonDownTextColor];

        /* If the highlighted title is set to _theme_asdf, look for a custom
         * button image called "asdf" and use that. Clear out this highlighted
         * title string. */

        NSString *hlTitle = [self titleForState:UIControlStateHighlighted];
        if ([hlTitle isEqualToString:@"_theme_add"] || [hlTitle isEqualToString:@"Add"]) {
            upImage = [theme rectButtonUpAdd];
            downImage = [theme rectButtonDownAdd];
        } else if ([hlTitle isEqualToString:@"_theme_remove"]) {
            upImage = [theme rectButtonUpRemove];
            downImage = [theme rectButtonDownRemove];
        } else {
            upImage = [theme rectButtonUp];
            downImage = [theme rectButtonDown];
        }

        [self setTitle:nil forState:UIControlStateHighlighted];
        upColor = [theme rectButtonUpTextColor];
        downColor = [theme rectButtonDownTextColor];

        [self setBackgroundImage:upImage forState:UIControlStateNormal];
        [self setBackgroundImage:downImage forState:UIControlStateHighlighted];
        [self setBackgroundImage:downImage forState:UIControlStateSelected];

        if (upColor) {
            [self setTitleColor:upColor forState:UIControlStateNormal];
            [self setTitleColor:[upColor colorByLighteningTo:0.5f] forState:UIControlStateDisabled];
        }
        if (downColor) {
            [self setTitleColor:downColor forState:UIControlStateHighlighted];
            [self setTitleColor:downColor forState:UIControlStateSelected];
        }  
}

任何帮助将不胜感激!

【问题讨论】:

  • 子类化 UIButton 并在您想要混合时使用该类以便只混合新类?

标签: objective-c uibutton uitableview swizzling


【解决方案1】:

我已经完成的快速修复,实现了我想要实现的目标。

代码: if ([self isMemberOfClass:[UIButton class]] && self.buttonType == UIButtonTypeCustom) {

    if (![[self titleLabel] text]) {

    } else {

        UIImage *upImage = [theme rectButtonUp], *downImage = [theme rectButtonDown];
        UIColor *upColor = [theme rectButtonUpTextColor], *downColor = [theme rectButtonDownTextColor];

        /* If the highlighted title is set to _theme_asdf, look for a custom
         * button image called "asdf" and use that. Clear out this highlighted
         * title string. */

        NSString *hlTitle = [self titleForState:UIControlStateHighlighted];
        if ([hlTitle isEqualToString:@"_theme_add"] || [hlTitle isEqualToString:@"Add"]) {
            upImage = [theme rectButtonUpAdd];
            downImage = [theme rectButtonDownAdd];
        } else if ([hlTitle isEqualToString:@"_theme_remove"]) {
            upImage = [theme rectButtonUpRemove];
            downImage = [theme rectButtonDownRemove];
        } else {
            upImage = [theme rectButtonUp];
            downImage = [theme rectButtonDown];
        }

        [self setTitle:nil forState:UIControlStateHighlighted];
        upColor = [theme rectButtonUpTextColor];
        downColor = [theme rectButtonDownTextColor];

        [self setBackgroundImage:upImage forState:UIControlStateNormal];
        [self setBackgroundImage:downImage forState:UIControlStateHighlighted];
        [self setBackgroundImage:downImage forState:UIControlStateSelected];

        if (upColor) {
            [self setTitleColor:upColor forState:UIControlStateNormal];
            [self setTitleColor:[upColor colorByLighteningTo:0.5f] forState:UIControlStateDisabled];
        }
        if (downColor) {
            [self setTitleColor:downColor forState:UIControlStateHighlighted];
            [self setTitleColor:downColor forState:UIControlStateSelected];
        }
    }   
}

所以,基本上,所有UIButtons 都有一个titleLabeltext,但UITableViewCellAccessoryType 没有。因此,我们取得了成功。这可能是一个 hack,但现在它已经帮助我完成了我需要的东西。

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 2015-09-07
    • 2012-11-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多