2012-03-27 2 views
1

내 응용 프로그램에 대한 "설정"테이블보기를 만들려고합니다. 나는 아이폰에 일반 설정과 같은 스타일로 그것을 모방하려고합니다. UITableCell에서 상속하여 내 자신의 사용자 지정 셀 클래스를 만들었습니다. 적절한 IBOulet을 주었고 스토리 보드에 연결했습니다. 나는 또한 내 tableViewControler에 스위치를 연결했지만 몇 가지 이유로 내 코드는 하나의 빈 셀만 리턴한다. (단 하나의 셀만이 내 설정에있는 모든 ATM에 문제가되지 않는다.) 나는 트리플을 점검하고 코드와 스토리 보드에서 동일한 셀 식별자를 사용하고 있는지 확인했습니다. 왜 내가 빈 셀을 돌려 받는지 누가 알 겠어?IOS 정적 테이블 사용자 지정 셀 전용 무작위 셀을 그립니다.

내 사용자 정의 셀에 대한 .h 파일이 있습니다.

@interface NHPSettingsCell : UITableViewCell 
@property (nonatomic,weak) IBOutlet UILabel *settingLabel; 
@property (nonatomic,strong) IBOutlet UISwitch *settingSwitch; 
@end 

내 문제 코드, 여기에 사용자 정의 셀 내 .H 파일입니다

#import "NHPSettingsCell.h" 

@implementation NHPSettingsCell 
@synthesize settingLabel, settingSwitch; 

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

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

    // Configure the view for the selected state 
} 

@end 

내 사용자 정의보기 컨트롤러 셀을 그리기에 대한 나의 방법 : 이제

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"SettingsCell"; 
    NHPSettingsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[NHPSettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellStyleDefault; 
    } 
    cell.settingLabel.text = @"firstSetting"; 

    //check the if user wants promt of disclaimer each time or not. 
    if([prefs boolForKey:@"firstSetting"] == YES){ 
     cell.settingSwitch.on = YES; 
    }else{ 
     cell.settingSwitch.on = NO; 
    } 

    return cell; 
} 

나를 귀찮게하는 건 내가 성공적으로 사용자 정의 셀을 사용하는 동적 테이블에 대한 cellForRowAtIndexPath 메소드를 구현할 수 있었기 때문입니다. 또한 기본 셀을 사용하는 정적 테이블의 코드를 구현했지만 사용자 정의 셀이있는 정적 테이블의 경우에는 작동하지 않는 것 같습니다. 다음은 동적 테이블에 내 사용자 정의 셀을 구현 한 코드입니다 (셀을 초기화 할 필요는 없지만 작동하는 방식에 유의하십시오).

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"InteractionResultCell"; 
    NHPResultCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    // Configure & Fill the cell 
    cell.leftLabel.text = [[resultsList objectAtIndex:indexPath.row] substanceName]; 
    cell.rightLabel.text = [[resultsList objectAtIndex:indexPath.row] substanceName2]; 
    NSString *color = [NSString stringWithFormat:@"%@", [[resultsList objectAtIndex:indexPath.row] color]]; 

    //Change a hex value to a readable 0x number to pass ot hte macro so we can go from a hex color to a RGB system. 
    NSScanner *scanner; 
    unsigned int tempint=0;  
    scanner = [NSScanner scannerWithString:color]; 
    [scanner scanHexInt:&tempint]; 

    cell.severityButton.backgroundColor = UIColorFromRGB(tempint); 


    return cell; 
} 

답변

8

두 가지 문제 : 정적 세포를 사용하는 경우

  • , 보기 컨트롤러 (numberOfRows, numberOfSections, cellForRow ...)에 데이터 소스 메서드를 구현하지 마십시오. 스토리 보드에 내장 된 데이터 소스가 재정의됩니다. 표에는 스토리 보드에서 제공하는 섹션, 행 및 콘텐츠가 있습니다.
  • 스토리 보드 (동적 프로토 타입 또는 정적 셀)에서로드 된 셀은 initWithStyle:이 아니라 initWithCoder:을 사용하여 초기화됩니다. awakeFromNib:은 설정 코드를 입력하기에 더 좋은 장소입니다.
+0

그게 효과가있다. 고맙습니다. – user597608

+0

initWithCOder 사용법에 대한 몇 가지 코드를 제공 할 수 있습니까? awakeFromNib와 동일합니다. 나는 물어 보는 것에 대해 사과하지만이 코드는 곧 발표 될 예정이며 atm 기능을 깨뜨리는 것이 두려워요. – user597608

+0

이것은 표준 init 메소드입니다. self = [super initWithCoder : aDecoder]; if (self) 등등 awakeFromNib도 표준 방법입니다. 내가 어떤 견본을 줄 수 있는지 모르겠다. – jrturton

0

dequeueReusableCellWithIdentifier:은 반복 된 메모리 할당을 방지하기 위해 셀이 이미 만들어진 경우에만 작동합니다. 먼저 셀을 만들지 않고 재사용 할 수 없습니다. xib에 생성 된 정적 셀이 기본 유형입니다. 그래서 사용자 정의 셀이있는 정적 테이블에서는 작동하지 않습니다. 당신이 당신의 사용자 정의보기 컨트롤러의 cellForRowAtIndexPath: 방법에서 수행 한 것처럼 재사용 후 세포 생성 코드를 추가

if (cell == nil) { 
    cell = [[NHPSettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.selectionStyle = UITableViewCellStyleDefault; 
} 

편집 - 사용자 정의 세포 init하려면 xib에서로드해야합니다.

+(NHPSettingsCell*) createTextRowWithOwner:(NSObject*)owner{ 
    NSArray* wired = [[NSBundle mainBundle] loadNibNamed:@"NHPSettingsCell" owner:owner options:nil]; 
    NHPSettingsCell* cell = (NHPSettingsCell*)[wired firstObjectWithClass:[NHPSettingsCell class]]; 

    return cell; 
} 

을 다음과 같은 사용자 정의보기 컨트롤러에서 호출 : 당신의 NHPSettingsCell.m에 다음 클래스 메서드를 추가

cell = (NHPSettingsCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
if (Nil == cell) { 
    cell = [NHPSettingsCell createTextRowWithOwner:self]; 
} 
+0

내 질문을 오해했습니다. 내 문제는 내 게시물 상단에서 첫 번째. 파일에있는 코드입니다. 하단에있는 파일이 아닙니다. 바닥에있는 것이 완벽하게 작동합니다. 코드는 이미 제 코드에 있습니다. 아니면 내가 당신의 의견을 오해 했습니까? – user597608

+0

나는 "MY Problem code is here"를 포함하도록 내 게시물을 변경하여 수정에 관심이있는 블록을 지정했습니다. – user597608