UITableViewCell 선택된 행의 텍스트 색상 변경
나는 tableview를 가지고 있는데 선택한 행의 텍스트 색상을 Red로 변경하려면 어떻게해야하는지 알고 싶습니다. 이 코드로 시도했습니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [localArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cityName = [localArray objectAtIndex:indexPath.row];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.textColor = [UIColor redColor];
//theCell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
(1) 행을 선택하면 텍스트 색상이 빨간색으로 변경되지만 다른 행을 선택하면 이전에 선택한 행의 텍스트가 빨간색으로 유지됩니다. 어떻게 해결할 수 있습니까?
(2) 표 텍스트 색상을 검정색으로 스크롤하면 어떻게 해결할 수 있습니까?
감사..
다음에서 수행하십시오 tableView:cellForRowAtIndexPath:
.
cell.textLabel.highlightedTextColor = [UIColor redColor];
( cell.text = ...
더 이상 사용하지 마세요 . 거의 2 년 동안 사용되지 않습니다. cell.textLabel.text = ...
대신 사용하세요 .)
으로 라파엘 올리베이라는 코멘트에 언급 된 셀의 selectionStyle가 동일한 경우, UITableViewCellSelectionStyleNone
이 작동하지 않습니다. 선택 스타일에 대해 스토리 보드도 확인하십시오.
셀 배경색을 변경하지 않고 텍스트 색상 만 변경하려는 경우. 이것을 사용할 수 있습니다.이 코드를 cellForRowAtIndexPath 메소드에 작성하십시오.
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];
나는 같은 문제가 있었다, 이것을 시도하십시오!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (id object in cell.superview.subviews) {
if ([object isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cellNotSelected = (UITableViewCell*)object;
cellNotSelected.textLabel.textColor = [UIColor blackColor];
}
}
cell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
그것은 당신의 (그리고 나의) 문제에 대한 해결책이 될 수 있습니다.
이미 서브 클래 싱중인 경우 메서드 UITableViewCell
에서 색상을 설정하는 것이 더 쉽고 / 깨끗합니다 awakeFromNib
(스토리 보드 또는 xib에서 인스턴스화한다고 가정).
@implementation MySubclassTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
self.customLabel.highlightedTextColor = [UIColor whiteColor];
}
@end
참고 URL : https://stackoverflow.com/questions/5841056/uitableviewcell-selected-rows-text-color-change
'developer tip' 카테고리의 다른 글
카메라 롤 액세스 권한 요청 (0) | 2020.11.29 |
---|---|
사용 가능한 메모리 C ++ / g ++를 얻는 방법은 무엇입니까? (0) | 2020.11.29 |
Chart.js-Y 축 서식 지정 (0) | 2020.11.29 |
삽입물을 수용하도록 UILabel 크기 조정 (0) | 2020.11.29 |
Dock 아이콘을 숨기는 방법 (0) | 2020.11.29 |