2009-08-25 4 views
6

내 문제와 비슷한 게시물을 찾았지만 동일하지는 않습니다.uitableviewcell에서 tableview 중복 행

내 앱에서 사용자는 여러 가지보기를 탐색하여 원하는 결과를 자세히 볼 수 있습니다. 사용자가 앞으로 나아갈 때, 뒤로, 앞으로 갈 때 등 행이 다시 그려지고 다시 쓰여지고 텍스트가 더 강하고 굵게 표시됩니다.

나는 게시물 중 일부에서 이것은 행을 만드는 방법과 관련이 있으며, cellforrowatindexpath 메서드 내에서 사용할 수있는 것을 발견했다.

사용자가 테이블 뷰간에 앞뒤로 이동할 때마다 행이 다시 채워지거나 다시 그려지지 않도록해야 할 일이 있습니까? 아래 코드에 뭔가를 추가하거나 viewwillappear 메서드에 무언가를 추가해야합니까 (현재 테이블의 뷰에 'reloaddata'가 있지만 도움이되지 않습니다)?

[cell.contentView addSubview:label]; 

당신은 새로운 세포의 여부 테이블 셀에 하위 뷰를 추가 : 때문에이 라인에

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20]; 
    label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f); 
    label.textColor = [UIColor blackColor]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.opaque = NO; 
    label.text = [mapareaArray objectAtIndex:indexPath.row]; 
    [cell.contentView addSubview:label]; 

    CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero]; 
    bgView.borderColor = [UIColor clearColor]; 
    bgView.fillColor = [UIColor whiteColor]; 
    bgView.position = CustomCellBackgroundViewPositionSingle; 
    cell.backgroundView = bgView; 
    return cell; 
} 

답변

11

당신이 겪고있는 문제는 다음과 같습니다

내 코드입니다 . 이전 셀 (재사용 가능한 풀에서 대기열에서 제외됨) 인 경우 셀에 다른 하위 뷰를 추가합니다.

대신 UILabel에 태그를 지정하고 해당 UILabel의 내용을 수정하려면 태그로 태그를 지정해야합니다. 추가 (및 모든 속성을 설정)과 경우 (셀 == 전무) 블록 내부의 UILabel의 태그 :

if(cell == nil) { 
    // alloc and init the cell view... 

    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.tag = kMyTag; // define kMyTag in your header file using #define 
    // and other label customizations 
    [cell.contentView addSubview:label]; // addSubview here and only here 
    ... 
} 

그런 다음 그것을 찾습니다

UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag]; 
label.text = [mapareaArray objectAtIndex:indexPath.row]; 

그리고 필요가 없습니다에 다시 추가 그것은 if (cell == nil) 블록 외부의 서브 뷰입니다. 하위 뷰는 이미 있습니다. (셀 뷰를 재사용하는 것이 왜 그렇게 올바르게하면 셀 뷰를 재사용하는 것이 훨씬 효율적입니다.).

+0

고맙습니다. 큰 도움. 이것에 관한 마지막 질문 하나 - 프로그래밍 배경이 없으므로 헤더 파일에서 #define을 사용하여 무슨 뜻인지 확실치 않습니다. 이 작업을 수행하는 구문은 무엇입니까? 나는 전에 그것을 보았고 헤더에 '#define kMyTag'를 넣으려고했으나 이것이 작동하지 않습니다 ...나는 당신이 kMyTag를 값으로 정의 할 필요가 있다고 가정하고 있지만 어떤 구문이 이것을 할 것인지 확신 할 수 없다. 도울 수 있니? – SKayser

1

.H 파일 은 '정의'헤더 파일의 맨 위에있는 # import를 문 다음에 넣어, 나는 그것을 정의하는 방법 밖에 모르기 때문에 0으로 넣어 :

#define kMyTag 0 

합니다. m 파일 사용자 의견에 따라이 섹션을 업데이트했지만 a) 테이블이 채워지지 않아 b) 사용자가 다음보기로 이동하여이보기로 돌아갈 때 "인스턴스로 전송 된 인식 할 수없는 선택기" , 그리고 c) 나는 두 개의 'return cell'을 넣어야했다. 항목 또는 넘어집니다. 나는 잘못된 순서로 사물을 가지고 있고, 사물을 제대로 초기화하지 않았다고 생각한다.

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

if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     UILabel *label = [[[UILabel alloc] init] autorelease]; 
     label.tag = kMyTag; // define kMyTag in your header file using #define 
     label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20]; 
     label.frame = CGRectMake(10.0f, 10.0f, 220.0f, 22.0f); 
     label.textColor = [UIColor blackColor]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.opaque = NO; 

     CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero]; 
     bgView.borderColor = [UIColor clearColor]; 
     bgView.fillColor = [UIColor whiteColor]; 
     bgView.position = CustomCellBackgroundViewPositionSingle; 
     cell.backgroundView = bgView; 

     [cell.contentView addSubview:label]; // addSubview here and only here 
     return cell; 
    } 
UILabel *label = (UILabel *)[cell.contentView viewWithTag: kMyTag]; 
label.text = [mapareaArray objectAtIndex:indexPath.row]; 
return cell; 
} 
+0

kMyTag의 값을 0으로 사용해야하는지 잘 모르겠습니다. 1이 모든 것의 기본값이 될 수 있으므로 1부터 시도해보십시오. 그 변경 후 if 블록 안의 리턴 셀을 제거하십시오. – tchen

+0

다시 한번 감사드립니다! 그게 지금 작동합니다! 감사합니다! – SKayser

0

그런 다음, 특정 셀에 대해있는 UITableViewCell의 하위 클래스를 만들려면 자신에게 때마다 하위 뷰를 추가 할 필요가 있는지 확인하기 위해 논리를 적용해야합니다. 또한 UITableviewcell의 prepareForReuse를 사용하고 그 시간 동안 서브 뷰를 제거한 후에 UILabel을 셀에 추가하려는 로직을 적용하십시오.

관련 문제