2009-11-18 3 views
4

initWithFrame이 3.0 이후에 사용되지 않으므로 initWithStyle을 사용하여 사용자 정의 TableViewCell을 만들려고합니다. 전에는 initWithFrame으로 모든 것이 잘 작동했습니다.3.0 이후에 initWithStyle을 사용하여 사용자 정의 TableViewCell을 만드는 방법

사용할 수있는 자습서 또는 샘플 코드가 있습니까? 감사.

+0

나는 이것을 알고 싶다. 암시 적 뷰가없는 스타일 만있는 경우 ... –

답변

7

UITableViewCell을 서브 클래 싱 한 다음 initWithStyle 메서드를 재정의합니다.

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

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
     self.selectionStyle = UITableViewCellSelectionStyleNone; 

     // Initialization code 
     msgText = [[UILabel alloc] init]; 
     [self.contentView addSubview:msgText]; 
    } 
    return self; 
} 

msgText는 클래스의 UILabel 속성이며 다른 위치에서 레이블의 텍스트 속성을 설정합니다. 원하는 뷰를 self.contentView에 추가 할 수 있습니다. 또한 텍스트 및/또는 이미지와 같은 컨텐트를 추가 할 때 각 하위 뷰의 프레임을 설정합니다.

+0

layoutSubviews에서 요소의 위치를 ​​정의 할 수 있습니까? tnx –

+1

물론입니다. UITableViewCell의 왼쪽 위를 기준으로 UITableViewCell 하위 뷰에 추가 할 요소의 프레임을 설정합니다. – acqu13sce

+1

initWithStyle 메서드가 호출되지 않습니다. tableView의 cellForRowAtIndexPath에서 무엇을합니까? – Michael

관련 문제