2012-08-13 7 views
19

서브 클래스가 UITableViewCell입니다. 내 하위 클래스에는 여러 개의 UILabel이 포함되어 있습니다. 내 하위 클래스가 내 모든 테이블보기 셀에 적용되는 기본 설정으로이 레이블을 초기화하기를 원합니다.initWithCoder가 항목을 올바르게 초기화하지 않는 이유는 무엇입니까?

나는 이것을 위해 initWithCoder을 사용해야한다고 읽었습니다. 내 initWithCoder 함수가 호출되고 있으며 함수의 각 행을 단계별로 실행할 수 있으며 동작을 통과 한 것처럼 보입니다. 내 응용 프로그램을 실행할 때 적용되는 속성이 표시되지 않습니다. 가장 눈에 띄게, 글꼴이 변경되지 않습니다. 나는 단지 내 UILabel에서 수정중인 속성 중 하나가 실제로 저장되거나 표시되지 않는다고 생각합니다.

저는이 서브 클래스를 Storyboard과 함께 사용하고 있습니다. 변경 사항이 Storyboard에 반영되지는 않지만 응용 프로그램이 실행될 때 코드가 실행 되더라도 반영되지 않습니다.

편집 : initWithCoder을 덮어 쓰려고하기 전에이 논리를 실행할 서브 클래스에 인스턴스 메서드가 있습니다.이 인스턴스 메서드를 cellForRowAtIndexPath 안에 호출하면됩니다. 이 방법은 효과가 있었지만이 하위 클래스의 논리를 자동으로 생성하는 것이 편리 할 거라고 생각했습니다.

아이디어가 있으십니까? 내 코드는 다음과 같습니다 :

#import "ThreadListCell.h" 

@implementation ThreadListCell 

- (id)initWithCoder:(NSCoder *)aDecoder { 

    if ((self = [super initWithCoder:aDecoder])) { 

     // adjust the font settings of the title 
     self.title.font = [UIFont fontWithName:@"SourceSansPro-Black" size:16]; 
     self.title.textColor = [UIColor colorWithWhite:0.267f alpha:1.0f]; 

     // adjust the font settings of the subtitle 
     self.text.font = [UIFont fontWithName:@"SourceSansPro-Light" size:14]; 
     self.text.textColor = [UIColor colorWithWhite:0.267f alpha:0.9f]; 
     self.text.textColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1]; 

     // adjust the font settings of the location 
     self.location.font = [UIFont fontWithName:@"SourceSansPro-Light" size:8]; 
     self.location.textColor = [UIColor colorWithWhite:0.267f alpha:0.9f]; 

     // adjust the UILabel settings of the title 
     self.title.numberOfLines = 0; 
     self.title.lineBreakMode = UILineBreakModeTailTruncation; 

     // adjust the UILabel settings of the subtitle 
     self.text.numberOfLines = 0; 
     self.text.lineBreakMode = UILineBreakModeTailTruncation; 

     // adjust the UILabel settings of the location 
     self.location.numberOfLines = 0; 
     self.location.lineBreakMode = UILineBreakModeTailTruncation; 
     self.location.textAlignment = UITextAlignmentRight; 

    } 

    return self; 

} 

@end 

그리고 헤더 : 메시지를받을 때 IBOutlets의

#import <UIKit/UIKit.h> 

@interface ThreadListCell : UITableViewCell 

@property (nonatomic, weak) IBOutlet UILabel *text; 
@property (nonatomic, weak) IBOutlet UILabel *title; 
@property (nonatomic, weak) IBOutlet UILabel *location; 

@end 

답변

45

없음이 설정되지 않은 - 당신이 "로 awakeFromNib"또는 "의 viewDidLoad"를 사용합니다 - 그런 일 - 출구에 접근하십시오. 즉, initWithCoder에서 다른 비 콘센트 재료를 설정할 수 있습니다.

+1

오, 확인. 'initWithCoder'를'awakeFromNib'으로 바꾸면 예상대로 작동하는 것 같습니다. 고마워요! – Ryan

+0

'cellForRowAtIndexPath'에 내 속성을 설정하기 전에'awakeFromNib'가 발생합니다. 이것은 아마도 "UILabel"의 현재 텍스트를 기반으로 크기 조정 작업을 안정적으로 수행 할 수 있기 때문에 내 인스턴스 메서드가 더 나은 방법이라고 생각하게합니다. – Ryan

+3

순서는 initWithCoder, awakeFromNib, cellForRowAtIndexPath 여야합니다. 일부 수학 값을 "사전 설정"해야하는 경우 init 루틴에서 수행 할 수 있습니다. 그러나 펜촉을 넣기 전까지는 콘센트에 액세스 할 수 없습니다. –

관련 문제