2017-02-02 2 views
0

UITableViewCellUILabel이 있습니다. 마지막 행 선택에서 모든 행의 UILabel 텍스트 색상을 변경해야합니다. 내가 어떻게 할 수 있니?UITableView 특정 행 선택에서 모든 행의 레이블 색 변경

+0

사용은 행 방법을 didselectselected 그냥 참 또는 거짓 설정하고 선택과 cellForRowAtIndexPath 체크 부울에서 하나 개의 인덱스 값을 설정 color 또는 lable –

+0

didSelectRowAt 메서드에서 마지막 행인지 여부를 확인합니다. 마지막 셀이 있으면 UITableView를 다시로드하십시오. 그런 다음 cellForRowAtIndexPath 메서드의 모든 셀에서 UILabel의 색을 변경합니다. tableview 업데이트를 다시로드하기 전에 마지막 셀을 두드린 후 알 수있는 플래그가 다시로드되었습니다. – Tejas

+0

@HimanshuMoradiya 실제로 사용자가 마지막 행을 다시 실제 색상으로 되돌리려면 실제로는 그 반대가 필요합니다. 지금 didselectselected 행 메서드를 사용하면 bool 값을 변경하고 tableview를 다시로드하는 중입니다. 그러나 이것은 모든 값의 색상을 채닝하는 것입니다. 이제는 diddeselect가 작동하지 않습니다 .. !! – Niharika

답변

-1

isLastRowSelected과 같은 부울을 설정하고 사용자가 마지막 행을 선택한 다음 tableView를 다시로드하면 true로 설정할 수 있습니다. cellForRowAtIndexPath: 콜백에서

if isLastRowSelected { 
    cell.label.textColor = UIColor.desiredColor 
} 
else { 
    cell.label.textColor = UIColor.defaultColor 
} 

그것이 도움이 될 것입니다 희망 조건을 적용 :

전체 설명 :

var isLastRowSelected = false 

다음 클래스의 저장 속성을 설정

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    // First add the selectedItem in your selectedItems and in the last add this 
    isLastRowSelected = selectedItems.count == datasource.count 
    tableView.reloadData() 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if isLastRowSelected { 
     cell.label.textColor = UIColor.desiredColor 
    } 
    else { 
     cell.label.textColor = UIColor.defaultColor 
    } 
} 
,210
+0

그녀는 모든 행의 텍스트 색상을 변경하고 싶습니다. 마지막 행 선택 –

+0

여러 항목을 선택하려고하십니까? – iProgrammer

+0

@iProgrammer 예. – Niharika

0
BOOL *selectlastindex; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    selectlastindex = NO; 
    // suggestionList is my NSArray (Data source) 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (selectlastindex == YES) { 
     if (indexPath.row == suggestionList.count - 1) { 
      suggestiontitle.textColor = [UIColor blackColor]; 
     }else{ 
      suggestiontitle.textColor = [UIColor grayColor]; 
     } 

    }else{ 
     suggestiontitle.textColor = [UIColor blackColor]; 
    } 
    return cell; 
} 

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"click"); 

    if (indexPath.row == suggestionList.count - 1) { 
     selectlastindex = YES; 
     [self.Tableview reloadData]; 
    }else{ 
     selectlastindex = NO; 
     [self.Tableview reloadData]; 
    } 
} 

출력 : CHECK OUTPUT

+0

감사합니다. 내가 확인하고 알려 드리겠습니다. 고마워요 – Niharika

+0

@Niharika 임플란트가 내 출력을 확인하기 전에 당신 wnat가 완벽하지 않습니까? –

+0

예. 이것은 동일합니다. – Niharika

0

하는 대신 willDisplayCell:에 색상 코드를 호출보십시오 :

- (void)tableView:(UITableView *)tableView 
    willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

// do your logic here: 
if (selectlastindex == YES) { 
     if (indexPath.row == suggestionList.count - 1) { 
      suggestiontitle.textColor = [UIColor blackColor]; 
     }else{ 
      suggestiontitle.textColor = [UIColor grayColor]; 
     } 

    }else{ 
     suggestiontitle.textColor = [UIColor blackColor]; 
    } 
}