2013-09-24 3 views
1

내 응용 프로그램에서 NSMutableArray에서 몇 가지 질문을하고 tableView 행에 표시하고 있습니다. 각 질문 (tableView 행 단추) 아래에 두 개의 단추를 사용했습니다. YES 및 NO 버튼을 누르면 TableRow 색상이 각각 녹색, 노란색으로 바뀝니다.UITableView 스크롤 쿼리에서 셀 색상 변경

문제는 특정 행의 버튼을 누르면 행이 녹색으로 변합니다. 그러나 스크롤 후 원치 않는 세포도 녹색/노란색으로 변합니다.

당신은 내 아래 코드를 확인하시기 바랍니다 그리고 당신은 새로 할당 된 셀의 배경색을 설정해야

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

contentView.hidden = YES; 
if(tableView == myChklist) 
{ 

    static NSUInteger const kQuestLabelTag = 1; 
    static NSUInteger const kYesButtonTag = 2; 
    static NSUInteger const kNoButtonTag = 3; 

    UILabel *QuestLabel = nil; 
    UIButton *YesButton; 
    UIButton *NoButton; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone;   //cell bg 
     //self.myChklist.backgroundColor = [UIColor clearColor]; 
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     { 

      QuestLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5,768,0)]; 

      //some logic code 
      [cell.contentView addSubview:QuestLabel]; 


      NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ; 
      CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(670, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; 

      UIButton *YesButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

          //some logic button code 

      [cell.contentView addSubview:YesButton]; 

      UIButton *NoButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      //some logic button code 

      [cell.contentView addSubview:NoButton]; 



    } 
    else 

    { 

     QuestLabel = (UILabel *)[cell.contentView viewWithTag:kQuestLabelTag]; 
     YesButton = (UIButton *)[cell.contentView viewWithTag:kYesButtonTag]; 
     NoButton = (UIButton *)[cell.contentView viewWithTag:kNoButtonTag]; 


    } 


    QuestLabel.text = [buildingQuest objectAtIndex:indexPath.row]; 

    QuestLabel.numberOfLines = 0; 

    NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(670, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; 
     CGRect rect = QuestLabel.frame; 
     rect.size.height = titleSize.height+10; 
     QuestLabel.frame = rect; 

     YesButton.frame = CGRectMake(10,titleSize.height+15,60,30); 
     NoButton.frame = CGRectMake(95,titleSize.height+15,60,30); 

    } 


if([self getCheckedForIndex:indexPath.row]==YES) 
    { 
     cell.backgroundColor = [UIColor greenColor]; 
     Cam_Button.hidden = NO; 

    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 


    if([self getCheckedForYellow_Index:indexPath.row]==YES) 
    { 
     cell.backgroundColor = [UIColor yellowColor]; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 


    return cell; 

    } 
} 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 


if([self getCheckedForIndex:indexPath.row]==YES) 
{ 
    cell.backgroundColor = [UIColor greenColor]; 
    Cam_Button.hidden = NO; 

} 
if([self getCheckedForYellow_Index:indexPath.row]==YES) 
{ 
    cell.backgroundColor = [UIColor yellowColor]; 
} 


if ([self.arrayCheckUnchek containsObject:indexPath]) 
{ 
    cell.backgroundColor = [UIColor greenColor]; 
    Cam_Button.hidden = NO; 

} 

    if ([arrayCheckUnchek2 containsObject:indexPath]) 
    { 
     cell.backgroundColor = [UIColor yellowColor]; 
    } 

} 



- (void)tableView: (UITableView*)tableView 
willDisplayCell: (UITableViewCell*)cell 
forRowAtIndexPath: (NSIndexPath*)indexPath 
{ 
UITableViewCell *Cell = [myChklist cellForRowAtIndexPath:indexPath]; 
//same code as didSelectRowAtIndexPath method 

} 



-(void)NoAction:(id)sender event: (id)event 
{ 
_cellButtonPress = YES; 
checkUncheck = YES; 
NSSet *touches = [event allTouches]; 
UITouch *touch = [touches anyObject]; 
CGPoint currentTouchPosition = [touch locationInView:self.myChklist]; 
NSIndexPath *indexPath = [self.myChklist indexPathForRowAtPoint: currentTouchPosition]; 
UIButton *button = (UIButton *)sender; 
UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 

cell.backgroundColor = [UIColor yellowColor]; 
[self checkedCellAtYellow_Index:indexPath.row]; 

[arrayCheckUnchek2 addObject:indexPath]; 
} 

답변

1

제안 할 수 없습니다. 그렇게하지 않으면 녹색으로 바뀐 세포가 재사용 될 때 녹색으로 유지됩니다.

if (cell == nil) { 
     cell.backgroundColor = [UIColor clearColor]; 
     ... 
+0

나는 ... 성공하지 못했습니다. –

+0

완료 :) 귀하의 솔루션 + 버튼 동작에서 [self.myChecklist reloadData] 설정. –