2011-12-14 4 views
0

UITableView의 첫 번째 행에 일부 UILabels를 삽입하려고하지만 예상치 못한 결과가 발생합니다. 간결함을 위해, 여기에 요약입니다 :UITableView의 0 행 사용자 정의

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

if (cell == nil) 
    cell = [[UITableViewCell alloc[ ..... 

cell.textLabel.text = @""; 

if (indexPath.row == 0) { 
    cell.backgroundColor = .... 
    UILabel* x = ..... 
    [x setTag:1]; 
    [cell insertSubview:x]; 
} 
else { 
    cell.textLabel.text = .... 
    for (UIView* view in cell.subviews) { 
     if (view.tag == 1) 
      [view removeFromSuperview]; 
    } 
} 

return cell 

} 어쨌든 요점을의

. 문제는 배경색이 올바르게 설정되어있는 동안 테이블보기를 스크롤 할 때마다 내가 삽입 한 커스텀 UILabel이 사라진다는 것입니다. 아마도 dequeueReusableCellWithIdentifier에 대한 오해에서 비롯된 것일 수도 있지만 관계없이 라벨이 다시 삽입 될 것입니다.

+0

를 사용 : cellForRowAtIndexPath를'대부분의 작동 시간, 그것을하는 * 공식 * 방법은 ['-tableView : willDisplayCell : forRowAtIndexPath :'] (http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference)에 있습니다. .html # // apple_ref/occ/intfm/UITableViewDelegate/tableView : willDisplayCell : forRowAtIndexPath :). –

+0

'insertSubview'? 코드의 예일 뿐이지 만 정확하기 만하면됩니다. 실제 코드를 복사하여 붙여 넣기 만하면 정확한 결과를 볼 수 있습니다. 또한 셀의 'contentView' 뷰를 셀에 직접 추가하지 않아야합니다. –

답변

4

가장 좋은 방법은 다른 셀 식별자를 사용하고 셀이 요청 될 때마다 많은 뷰를 생성, 추가 및 제거하는 대신 셀이 생성 될 때 셀 전체를 초기화하는 것입니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString* normalCellIdentifier = @"Normal Cell"; 
    static NSString* specialCellIdentifier = @"Special Cell"; 

    if (indexPath.row == 0) { 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:specialCellIdentifier]; 

    if (cell == nil){ 
     cell = [[UITableViewCell alloc[ ..... 
     cell.backgroundColor = .... 
     UILabel* x = ..... 
     [cell insertSubview:x]; 
    } 

    return cell 
    } else { 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier]; 

    if (cell == nil) 
     cell = [[UITableViewCell alloc[ ..... 
    cell.textLabel.text = .... 
    return cell; 
    } 
} 
1

라벨과 같은 간단한 것을 추가하려는 경우 다른 셀 클래스가 필요하지 않습니다.

이 시도 :

/* ARC code */ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    if (0 == indexPath.row) { 
     cell.textLabel.text = @"Zero"; 
     UILabel *lbl = [[UILabel alloc] initWithFrame:(CGRect){.size={80, 20}}]; 
     lbl.backgroundColor = [UIColor redColor]; 
     lbl.textColor = [UIColor blackColor]; 
     lbl.text = @"@@@@"; 
     [cell addSubview:lbl]; 
    } else { 
     cell.textLabel.text = @"Not zero"; 
    }  
    return cell; 
} 

그런 다음, 다른 곳에서 약 dequeueReusableCellWithIdentifier: 읽어 보시기 바랍니다.

또한 유사한 경우에 UIView- (UIView *)viewWithTag:(NSInteger)tag 이 있으며 하위 클래스는 구현할 필요가 없습니다.

1

시스템의 작업을 줄이기 위해 뷰의 인스턴스화를 최소화하고 최소한으로하는 것이 좋습니다.

@ EmilioPelaez에는 하나의 솔루션이 있지만 코드가 반복적으로 여러 개의 이탈로 인해 꽤 못생긴다는 ​​단점이 있습니다. 당신이 무거운를하고 일반적인 형태의 tableview 셀을 설정하는 곳

나는 매우 행복이 if (!cell) { ... } 사이의 비트처럼 그것의이

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

    const NSInteger tag = 1; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
             reuseIdentifier:reuseIdentifier] autorelease]; 
     UILabel *label = [[UILabel alloc] initWithFrame:someFrame]; 
     label.hidden = YES; 
     label.tag = tag; 
     [cell.contentView addSubview:label]; 
     [label release]; label = nil; 
    } 

    cell.textLabel.text = nil; 
    UILabel *label = [cell.contentView viewWithTag:tag]; 

    if (0 == indexPath.row) { 
     cell.backgroundColor = [UIColor greenColor]; 
     label.hidden = NO; 
    } else { 
     cell.backgroundColor = [UIColor redColor]; 
     cell.textLabel.text = @"Some text"; 
     label.hidden = YES; 
    } 

    return cell; 
} 

생각해처럼하고있는 것입니다.

그런 다음보기 구성 요소를 구성하는 것입니다.

는 세포가 너무 많이 다를 경우 아마 다른 재사용 식별자로가는이라고 생각하고 -tableView`에서 셀의 배경색을 설정하는 동안 아마도 UITableViewCell의 서브 클래스 또는 xib

관련 문제