UIButton调配导致UITableViewCellAccessoryType的问题

时间:2012-04-13 03:15:33

标签: objective-c uibutton uitableview swizzling

UIButtonbuttonType为真时,我有一个让UIButtonTypeCustom移动的课程。 但是,在使用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];
        }  
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我做过的快速解决方案可以完成我想要实现的目标。

代码:     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都有titleLabel textUITableViewCellAccessoryType没有{{1}}。因此,我们取得了成功。它可能是一个黑客,但现在它帮助我完成了我需要的东西。