【问题标题】:UITableViewCell selected row's text color changeUITableViewCell 选中行的文字颜色变化
【发布时间】:2011-08-16 00:16:33
【问题描述】:

我有一个表格视图,我想知道如何更改所选行的文本颜色,比如 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) 当我滚动表格文本颜色变为黑色时如何解决?

谢谢..

【问题讨论】:

    标签: ios objective-c iphone uitableview


    【解决方案1】:

    我遇到了同样的问题,试试这个!

    -(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];
    }
    

    这可能是您(和我)问题的解决方案。

    【讨论】:

      【解决方案2】:

      如果您只想更改文本颜色,而不更改单元格背景颜色。你可以用这个。把这段代码写在cellForRowAtIndexPath方法中

      UIView *selectionColor = [[UIView alloc] init];
      selectionColor.backgroundColor = [UIColor clearColor];
      cell.selectedBackgroundView = selectionColor;
      cell.textLabel.highlightedTextColor = [UIColor redColor];
      

      【讨论】:

      • 我正在使用这种方法,但在 iOS 10 上设置 selectedBackgroundView 颜色会导致表格分隔符在选择时消失。我仍在努力解决这种副作用的好方法。
      【解决方案3】:

      tableView:cellForRowAtIndexPath: 中执行此操作:

      cell.textLabel.highlightedTextColor = [UIColor redColor];
      

      (并且不要再使用cell.text = ...。它已经被弃用了将近2年。改用cell.textLabel.text = ...。)


      正如 cmets 中提到的 Raphael Oliveira ,如果您的单元格的 selectionStyle 等于 UITableViewCellSelectionStyleNone 这将不起作用。检查 Storyboard 以及选择样式。

      【讨论】:

      • 在我选择另一行之前颜色应该是红色的!
      • 值得一提的是,如果您的单元格的 selectionStyle 等于 UITableViewCellSelectionStyleNone 这将不起作用。您可能没有添加此代码,但请签入 Storyboard,以免像我一样浪费大量时间试图弄清楚为什么这不起作用。
      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多