2012-04-13 1 views
0

buttonTypeUIButtonTypeCustom 일 때 UIButton이 깜박 거리는 클래스가 있습니다. 그러나 UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryDisclosure도 마찬가지입니다. 무슨 일이 일어나고 있는지 어떤 이유인지, 그것은 무엇인가 이유 때문에 그것들을 뒤집어서 accessoryType 뒤에 맞춤 배경을 추가하는 것입니다.UIButton swizzling이 UITableViewCellAccessoryType의 문제를 일으킬 수 있습니다.

내가해야 할 일은 내가 뒤죽박죽하려는 UIButtonUITableViewCellAccessoryType인지 아닌지 확인하는 것입니다. 그러나 나는 그런 일을하는 방법을 알지 못합니다.

다음은 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]; 
     } 
} 

어떤 도움을 주시면 감사하겠습니다.

+0

UIButton을 서브 클래스로 만들고, 그 클래스를 사용하여 새로운 클래스 만 쓸어 넘기시겠습니까? – lnafziger

답변

0

내가 성취 한 것을 달성 한 빠른 수정.

코드 : ([자기 isMemberOfClass : [있는 UIButton 클래스] & & 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 모든 text 만있는 titleLabelUITableViewCellAccessoryType 님의 이야기는 아닙니다. 그러므로 성공을 거두었습니다. 해킹일지도 모르지만 지금은 필요한 것을 완료하는 데 도움이되었습니다.