首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIButton swizzling导致UITableViewCellAccessoryType的问题

UIButton swizzling导致UITableViewCellAccessoryType的问题
EN

Stack Overflow用户
提问于 2012-04-13 11:15:33
回答 1查看 262关注 0票数 0

我有一个类,当buttonTypeUIButtonTypeCustom为真时,它会混淆UIButton的。然而,当涉及到UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryDisclosure时,这也是真的。现在发生的情况是,由于某些原因,它在accessoryType后面添加自定义背景等等。

我需要做的是检查我正在尝试的UIButton是否是一个UITableViewCellAccessoryType,但是我不知道如何做这样的事情。

下面是我用来搅乱UIButton的函数的内部结构。

代码语言:javascript
复制
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];
        }  
}

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-13 11:35:37

我所做的一个快速修复完成了我想要实现的目标。

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

代码语言:javascript
复制
    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都有一个带有texttitleLabel,但UITableViewCellAccessoryType没有。因此,我们成功了。这可能是一个技巧,但现在它帮助我完成了我需要的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10134769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档