2011-09-07 3 views
0

왜 여기에서 왜 그런 일이 일어나는지 알 수 없습니다. 색을 변경하기 위해 목표로 삼을 특정 색인을 식별합니다. 그리고 내부 :indexpath.row == ...를 사용하여 색상 별 셀의 텍스트 UITableView, 둘 이상의 셀에 색상 지정

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

나는 다음과 같은 실행

if ([savedPRIndex intValue] == indexPath.row) { 
     [customCell togglePR:TRUE]; 
     NSLog(@"TRUE"); 
    }else{ 
     [customCell togglePR:FALSE]; 
    } 

사용자 정의 셀에서

- (void)layoutSubviews{ 

    [super layoutSubviews]; 
    CGFloat xPosition = 20.0f; // Default text position 

    if(prToggle){ 
     xPosition = -20.0f; 
     cellText.textColor = [UIColor colorWithRed:0x24/255.0 green:0x9e/255.0 blue:0xd6/255.0 alpha:1.0];  
     UIImage* img = [UIImage imageNamed:@"pr_icon.png"]; 
     CGRect rect = CGRectMake(272.0f, 14.0f, img.size.width/2, img.size.height/2); 
     UIImageView* imgView = [[UIImageView alloc] initWithFrame:rect]; 
     [imgView setImage:img]; 

     [self addSubview:imgView]; 
     [imgView release]; 
    } 

    CGRect textLabelFrame = self.cellText.frame; 
    textLabelFrame.origin.x = xPosition; 
    self.cellText.frame = textLabelFrame; 
} 

-(void)togglePR:(BOOL)TF{ 
    prToggle = TF; 
} 

정의 셀 togglePR은 텍스트 색상을 변경할 수있는 유일한 장소입니다, 사람이 어떤 아이디어?

무슨 일이 일어나는지 해독하는 데 도움이 될 경우 더 많은 코드를 게시 할 수 있습니다.

추가 코드

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *customCell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (customCell == nil) { 
     customCell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSLog(@"savedPRIndex : %@", savedPRIndex); 
    NSLog(@"Index Path : %i", indexPath.row); 

    [customCell togglePR:[savedPRIndex intValue] == indexPath.row]; 

    // Configure the cell... 
    customCell.cellText.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"Value"]; 
    customCell.cellPower.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"PowerValue"]; 
    customCell.cellSplit.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"Split"]; 

    return customCell; 
} 
+0

세포를 재사용 할 수 있습니다. 또한 뷰의 수명 동안 여러 번 호출 될 수 있기 때문에'-layoutSubviews'에 뷰를 추가해서는 안됩니다. 표 셀에는 이미 이미지보기가 있으므로 어쨌든이를 사용해야합니다. –

답변

1

행방은 두 번째 if 문입니까? 처음에 셀을 작성하는 경우이 경우 셀을 다시 사용할 때 여러 셀이 설정 될 수 있습니다. 귀하의 cellForRowAtIndexPath는 다음과 같은 구조가 있어야합니다 셀 객체가 리턴 된 경우

  • 디큐 세포
  • 체크, 새하지 않을 경우
  • 구성 셀 (디큐 셀 또는 새로 생성 된 세포 중 하나를) 초기화를 당신의 업데이트 된 코드를 볼 수 :

내 생각 엔 당신의 코드는 위의 부분이 아닌 제 3 부

편집에 있다는 것이다. 토글을 설정하고 나면 토글을 해제하는 메커니즘이없는 것처럼 보입니다 (예 : 생성 된 하위 뷰를 제거하는 등). 토글 세트를 사용하여 셀을 dequeuing 할 수 있지만 NO로 설정하면 아무 효과가 없습니다.

+0

혼란스러워하는'[tableview reloadData];를 사용하기 시작할 때만 문제가있는 것 같습니다 –

+0

토글이 NO로 설정되었을 때 YES로 설정된 토글을 가진 셀은 무엇을 기대합니까?이전에 추가 된 이미지보기가 계속 표시됩니다. – jrturton

0

셀을 대기열에서 제외하려고 할 때 충분한 코드가 표시되지 않았고 그 코드가 if(!cell) 체크 안에 포함되지 않았을 가능성이 있습니다. 즉, 셀을 처음 생성 할 때만 실행되고 대기열이 제거되지 않은 셀은 실행되지 않습니다.

이제 측면 오브젝티브 C를 사용 YES 메모, 대신 TRUEFALSE, 당신은 부울을 설정해야 할 때, 체크하면 부울 표현식이 작동하기 때문에 사용할 필요가 없다의 NO.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableview dequeue...:@"blah"]; 
    if(!cell) 
    { 
     //Create cell 
     //Do not set the colors here 
    } 

    //Set the colors here (no need for an if check) 
    [customCell togglePR:[savedPRIndex intValue] == indexPath.row]; 
} 

편집 :

당신이 PR 플래그를 설정할 때 - (void)setNeedsLayout 또는 - (void)setNeedsDisplay를 호출해야합니다 당신의 갱신을 바탕으로

.

-(void)togglePR:(BOOL)TF{ 
    prToggle = TF; 
    [self setNeedsDisplay]; 
} 
+0

당신이 볼 수 있다면 위의 코드를 업데이트하십시오. –

+0

PR 플래그를 변경할 때 setNeedsLayout 또는 setNeedsDisplay를 호출 해보십시오. – Joe

+0

행운을 빕니다. 통화 할 때 무엇을합니까? –