2012-02-21 2 views
4

내 TableView를 더 이상 사용되지 않는 initWithFrame:reuseIdentifier:에서 업데이트하고 싶습니다.initWithFrame 대 initWithStyle

내 tableview는 맞춤 셀을 사용합니다.

어디서나 initWithStyle:을 사용하고 어떤 방식 으로든 동작이나 셀을 변경하지 않는다는 것을 initWithFrame:CGRectZero reuseIdentifier:이라고합니다.

그러나 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:으로 건물을 만들면 셀이 공백이됩니다. 즉, 맞춤 스타일 셀이 작동하지 않습니다 (일부 스타일로 초기화 되었기 때문에)?).

셀을 초기화 한 후 (큐에서 대기열에서 제외하지 않은 경우) 셀에 텍스트를 설정합니다. 그러나 이것들은 initWithStyle:reuseIdentifier:을 사용할 때 설정되지 않지만 initWithFrame:CGRectZero과 작동합니다. 사용 된 init 메소드 (initWithStyle)를 제외하고는 코드가 변경되지 않습니다. 전지 후 넣어

이 행이 생성 (또는 재사용)된다

cell.newsItemNameLabel.text = @"test"; 
NSLog(@"NewsItemName: %@",cell.newsItemNameLabel.text); 

결과 "NewsItemName : (널)"에

누구 아이디어가있다? 이 둘의 진정한 차이점은 무엇입니까?

+1

난 당신이 클래스의 자세한 내용을 게시해야합니다 생각합니다. – picciano

답변

3

cellForRowAtIndexPath의 구현은 다음과 유사합니다 감사합니다 CustomCell 사용자 정의 세포의 클래스의 이름입니다

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    CustomCell *cell = (CustomCell *)(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell. 
    cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail"); 
    return cell; 
} 

. 이 구현은 ARC (Automatic Reference Counting)를 사용합니다. 이 기능을 사용하지 않는 경우 셀 할당에 autorelease 호출을 추가하십시오.

CustomCellinitWithStyle 구현 :

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     //do things 
    } 
    return self; 
} 
+0

오! 이제 나는 어리 석다. 두 번째 편집을 보았을 때 CustomCell의 메서드도 물론 변경되었습니다. 정말 고맙습니다. 나는 명성을 얻지 못해서 당신을 분명히 업신 여길 수는 없지만, 나는 심령으로 당신에게 사랑의 힙을 보낼 것입니다. – Ostmeistro

+0

설명대로 정확하게했지만 사용자 정의 셀이 초기화되지 않습니다. 내가 잘못하면 무엇이 잘못되었는지 알 수 없습니다. Fscheidl, 내 프로젝트 이메일을 보낼 수 있다고 생각하니? – Michael

+0

확실히, 나는 그것을 들여다 볼 수 있었다 - 나는 다만 나의 전자 우편 주소를 온라인으로 배치하는 것이 편안하지 않다. 프로젝트를 어떻게 보내시겠습니까? – fscheidl

관련 문제