UITableViewCell
에 UILabel
이 있습니다. 마지막 행 선택에서 모든 행의 UILabel 텍스트 색상을 변경해야합니다. 내가 어떻게 할 수 있니?UITableView 특정 행 선택에서 모든 행의 레이블 색 변경
답변
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
그녀는 모든 행의 텍스트 색상을 변경하고 싶습니다. 마지막 행 선택 –
여러 항목을 선택하려고하십니까? – iProgrammer
@iProgrammer 예. – Niharika
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
하는 대신 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];
}
}
사용은 행 방법을 didselectselected 그냥 참 또는 거짓 설정하고 선택과 cellForRowAtIndexPath 체크 부울에서 하나 개의 인덱스 값을 설정 color 또는 lable –
didSelectRowAt 메서드에서 마지막 행인지 여부를 확인합니다. 마지막 셀이 있으면 UITableView를 다시로드하십시오. 그런 다음 cellForRowAtIndexPath 메서드의 모든 셀에서 UILabel의 색을 변경합니다. tableview 업데이트를 다시로드하기 전에 마지막 셀을 두드린 후 알 수있는 플래그가 다시로드되었습니다. – Tejas
@HimanshuMoradiya 실제로 사용자가 마지막 행을 다시 실제 색상으로 되돌리려면 실제로는 그 반대가 필요합니다. 지금 didselectselected 행 메서드를 사용하면 bool 값을 변경하고 tableview를 다시로드하는 중입니다. 그러나 이것은 모든 값의 색상을 채닝하는 것입니다. 이제는 diddeselect가 작동하지 않습니다 .. !! – Niharika