2013-01-19 3 views
3

이상한 일이 생겼습니다. 사용자 정의 tablecell을 만들려고했지만 initWithStyle이 호출되지 않았습니다.initWithStyle : (UITableViewCellStyle)이 호출되지 않았습니다.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

내 테이블 셀은 정상 같습니다 내가 Customcell 펜촉로드하기 위해 노력하고있어 어떻게

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     NSLog(@"xx1111"); 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *TFDCustomCell = @"TFDCell"; 
    TFDCell *cell = (TFDCell *)[tableView dequeueReusableCellWithIdentifier:TFDCustomCell]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TFDCell" 
                owner:self options:nil]; 
     for (id oneObject in nib) if ([oneObject isKindOfClass:[TFDCell class]]) 
      cell = (TFDCell *)oneObject; 
    } 

return cell; 

} 

을하지만 NSLog(@"xx1111"); doenst 내 로그에 나타납니다. NSLog를 'setSelected'에 놓으면 '괜찮습니다.'

답변

13

이 솔루션은 당신은 너무 initWithStyle 사용자 정의 셀의 해고되지 않습니다 initWithStyle와 테이블의 셀을 초기화하지 않을

-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    NSLog(@"initWithCoder"); 

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

    } 
    return self; 
} 
5

NBR에서 뷰 (현재 사례의 셀)를로드하면 알 수 있듯이 initWithStyle: 메서드는 호출되지 않습니다. 오버플로 awakeFromNib: 메서드 대신 사용자 지정 초기화를 수행하십시오.

+0

nib 파일에서 객체를 아카이브 해제 할 때 호출되는'initWithCoder'를 무시할 수도 있습니다. –

+0

고마워, upvoted. – Nathan

0

간단했다. 그러나 awakeFromNib은 초기화 호출 dequeueReusableCellWithIdentifier에 대해 호출됩니다.

+0

2 년 전 Rost의 대답은 이미 awakeFromNib을 설명합니다 ... – Paul

+0

'(셀 == nil) { NSArray * nib = [[NSBundle mainBundle] loadNibNamed : @ "TFDCell" 소유자 : 자체 옵션 : 무]; if (id oneObject in nib) if ([oneObject isKindOfClass : [TFDCell 클래스]] cell = (TFDCell *) oneObject; } ' 은 iOS 6 이상에서는 건너 뛸 수 있으며 ** dequeueReusableCellWithIdentifier **는 ** awakeFromNib **을 호출합니다. 그래서, @Paul, 이것은 Rost의 설명 상단에 약간 여분 것 같습니다. 또한, 적은 점수의 Rost 원인에 대한 의견을 추가 할 수 없습니다. – zeeawan

+0

그래서 새로운 버전 (지금은 ios8)에서는 다른 방법을 사용하여 답변에 넣을 수 있기 때문에 2 년 후 유용한 이유를 사람들이 이해할 수 있도록 분명히해야합니다. – Paul

관련 문제