2011-11-13 1 views
1

사용자가 테이블을 아래로 위로 스크롤 할 때 UITableViewCell 설치 문제가 발생합니다. 그룹화 된 표가 다른 셀로 스크롤되고 다시 해당 셀로 돌아 오면 셀은 변경 내용을 잃어 버립니다 (빨간색/녹색 배경 이미지는 -didSelectRowAtIndexPath으로 설정 됨). 나는 질문 아래의 코드를 사용하여 두 개의 섹션 4 응답 선택을 표시하고 있습니다UITableViewCell 테이블이 위로 스크롤되어 해당 셀에 돌아올 때 배경 이미지 문제가 발생했습니다.

는 .. 사용자가 선택 중 하나를 선택

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *kCustomCellID = [NSString stringWithFormat:@"CustomCellID%d", ((100 * indexPath.section) + indexPath.row)]; 

    UITableViewCell *cell = nil;//[tableView dequeueReusableCellWithIdentifier:kCustomCellID]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    if (indexPath.section == 0) { 
     cell.textLabel.text = questionText; 
     cell.textLabel.textAlignment = UITextAlignmentCenter; 
     cell.textLabel.font = FONT_QUESTIONTEXT; 
     cell.textLabel.numberOfLines = 0; 
    } 
    else { 
     cell.textLabel.text = [self.answerChoices objectAtIndex:indexPath.row]; 
     cell.textLabel.font = FONT_ANSWERTEXT; 
     cell.textLabel.numberOfLines = 0; 

     ConfigurationBase *configBase = [[ConfigurationBase alloc] initWithConfigurationType:kConfigTypeQuiz]; 

     NSString *backgroundImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_BACKGROUND]; 
     NSString *leftImage = [configBase.configInfo valueForKey:[choicesLeftImages objectAtIndex:indexPath.row]]; 
     [configBase release]; 

     cell.backgroundColor = [UIColor clearColor]; 
     UIImageView *bgView = [[UIImageView alloc] initWithFrame:cell.frame]; 
     bgView.image = [UIImage imageNamed:backgroundImageName]; 
     cell.backgroundView = bgView; 
     [bgView release]; 

     cell.imageView.image = [UIImage imageNamed:leftImage]; 
    } 

    return cell; 
} 

그리고는 나는 배경 색상의 옳고 그름 선택을 변경 녹색 및 빨강 색상. 2 행의 배경을 변경하는 코드는 다음과 같습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 1) { // Answer choice section. 
     NSString *correctAnswerStr = [quizSession fetchCorrectAnswer]; 
     NSInteger correctAnswerIndex = -1; 
     ConfigurationBase *configBase = [[ConfigurationBase alloc] initWithConfigurationType:kConfigTypeQuiz]; 

     NSString *correctAnsImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_CORRECT_BACKGROUND]; 
     NSString *wrongAnsImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_WRONG_BACKGROUND]; 
     [configBase release]; 

     UITableViewCell *answerCell = [tableView cellForRowAtIndexPath:indexPath]; 

     answerCell.backgroundColor = [UIColor clearColor]; 
     UIImageView *bgView = [[UIImageView alloc] initWithFrame:answerCell.frame]; 
     bgView.image = [UIImage imageNamed:wrongAnsImageName]; 
     answerCell.backgroundView = bgView; 
     [bgView release]; 

     for (NSString *answerChoice in answerChoices) { 
      ++correctAnswerIndex; 

      if ([answerChoice isEqualToString:correctAnswerStr]) { 
       // Correct Index obtained, so set the background color for it. 
       NSIndexPath *correctIndexPath = [NSIndexPath indexPathForRow:correctAnswerIndex 
                    inSection:indexPath.section]; 
       UITableViewCell *correctAnswerCell = [tableView cellForRowAtIndexPath:correctIndexPath]; 

       correctAnswerCell.backgroundColor = [UIColor clearColor]; 
       UIImageView *bgView = [[UIImageView alloc] initWithFrame:correctAnswerCell.frame]; 
       bgView.image = [UIImage imageNamed:correctAnsImageName]; 
       correctAnswerCell.backgroundView = bgView; 
       [bgView release]; 

       break; 
      } 
     } 

     // Enable the 'next' button, allowing user to go for next question. 
     self.navigationItem.rightBarButtonItem.enabled = YES; 
     // Disable selection of answer until user go to next question. 
    tableView.allowsSelection = NO; 
} 

아무도 도와 줄 수 있습니까? TIA.

감사합니다.

답변

0

배경 이미지 이름에 대한 정보를 검색하는 방법은 매우 복잡하고 오류가 발생하기 쉽습니다. 적절한 위치에 NSLog을 삽입하거나 디버거를 체크인하여 모든 것이 제대로 작동하는지 확인하십시오.

셀의 backgroundView 속성을 처리 할 때는이 코드를 cellForRowAtIndexPath 대신 tableView:willDisplayCell:forRowAtIndexPath:에 넣어야합니다. 보다 간단한 접근 방법으로 시간을 절약 할 수 있습니다.

관련 문제