UITableViewCell выбрал изменение цвета текста строки
У меня есть табличное представление, и я хочу знать, как изменить выбранный цвет текста строки, скажем, на 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) Когда я просматриваю изменение цвета текста таблицы на черный цвет, как это решить?
Спасибо..
Ответы
Ответ 1
Сделайте это в tableView:cellForRowAtIndexPath:
:
cell.textLabel.highlightedTextColor = [UIColor redColor];
(И не используйте cell.text = ...
больше, он устарел почти 2 года. Вместо этого используйте cell.textLabel.text = ...
.)
Как Рафаэль Оливейра, упомянутый в комментариях, если selectionStyle вашей клетки равен UITableViewCellSelectionStyleNone
, это не сработает. Проверьте раскадровку и стиль выбора.
Ответ 2
Если вы хотите изменить только цвет текста, без изменения цвета фона ячейки. вы можете использовать this.Write этот код в методе cellForRowAtIndexPath
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];
Ответ 3
У меня была такая же проблема, попробуйте это!
-(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];
}
Это может быть решением вашей (и моей) проблемы.
Ответ 4
Если вы уже подклассифицируете 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