【问题标题】:Long label "..." not changing colour with the rest of the text长标签“...”不随文本的其余部分改变颜色
【发布时间】:2025-12-09 16:20:11
【问题描述】:

我在UITableViewCell 中有一个UILabel。当用户按下它时,我会更改此标签的颜色,并在用户再次按下时将其更改回来。 这适用于所有标签,除非此标签太长并且文本被“...”-LoremIpsum ...

由于某种原因,这 3 个点不会与文本的其余部分一起改变颜色

任何想法我怎样才能使它们与文本的其余部分一起改变? 我这是框架中的错误吗?

// Configure the cell...

if([self.memberships indexOfObject:@(selectedDiscountProgram.uidValue)]!= NSNotFound){
    cell.nameLabel.textColor = RabatOrangeColor;
    [cell.backgroundImageView setImage:[UIImage rectImageWithGradientFromColor:[UIImage colorWithHex:0xDDDDDD] toColor:[UIImage colorWithHex:0xEEEEEE]]];
    [cell.memberButton setImage:[UIImage imageNamed:@"tjek"] forState:UIControlStateNormal];
    cell.memberButton.imageEdgeInsets =UIEdgeInsetsMake(0, 0, 0, 9.0f);
    //cell.memberButton.frame = CGRectMake(cell.memberButton.frame.origin.x, cell.memberButton.frame.origin.y, 36.0, 36.0);
    [cell.memberButton setTitle:[NSString stringWithFormat:@""] forState:UIControlStateNormal];

}else{


    cell.nameLabel.textColor = RabatGrayColor;
    [cell.backgroundImageView setImage:[UIImage rectImageWithGradientFromColor:[UIImage colorWithHex:0xFFFFFF] toColor:[UIImage colorWithHex:0xF0F0F0]]];
    [cell.memberButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [cell.memberButton setTitle:[NSString stringWithFormat:@""] forState:UIControlStateNormal];
    [cell.memberButton setTitleColor:RabatGrayColor forState:UIControlStateNormal];
}

【问题讨论】:

  • 你能在你切换颜色的地方显示你的代码吗?通过在选择时更改颜色,我没有任何问题。
  • @Justafinger 添加了 cellForRowAtIndexPath 中的代码。
  • 我也遇到了同样的问题。由于某种原因,“...”不会改变颜色(iOS 7.1)
  • 您知道 Apple 是否有错误报告吗?
  • 我不知道,我很快就会提交一份。与此同时,我在下面发布了一个对我有用的答案,直到 Apple 修复它。

标签: ios objective-c uitableview uilabel uicolor


【解决方案1】:

在自定义单元格中添加一个布尔属性只是为了知道天气它是否被选中 例子

在您的自定义单元格CustomCell.h 文件中

  #import <UIKit/UIKit.h>

  @interface CustomCell : UITableViewCell
  @property (nonatomic, retain) UILabel *nameLabel;
  ....
  ....
  .... //your other properties for custom cell 
  @property (nonatomic, assign) BOOL isSelected; //add this boolean variable
  @end

CustomCell.m

@synthesize isSelected;

在初始化时将其设置为

self.isSelected = NO;//because at the beginning no cell is selected

在控制器中,tableview 委托方法如下所示

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
      if(cell == nil)
      {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
      }
      cell.nameLabel.text = @"some long text some long text some long text some long text some long text some long text";
      //....other settings
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      cell.nameLabel.textColor = [UIColor blackColor]; //initially set to balck color
      return  cell;

  } 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; //get the cell of the selected index path
     if(cell.isSelected)
     {
        cell.isSelected = NO; //if it is selected set it bool to NO and change color to black
        cell.nameLabel.textColor = [UIColor blackColor];
     }
     else
     {
       cell.isSelected = YES;  //initially it is not selected so select it and set bool to YES and change the colour
       cell.nameLabel.textColor = [UIColor orangeColor];
     }

 }

【讨论】:

    【解决方案2】:

    也许您可以在自定义单元格中添加一个布尔值来指示状态,如 isOrange,那么您将拥有:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
        if ([cell isOrange]) {
            cell.nameLabel.textColor = RabatGrayColor;
        }else{
            cell.nameLabel.textColor = RabatOrangeColor;
        }
        cell.isOrange = !cell.isOrange;
    }
    

    您可以在 cellForRowAtIndexPath 中添加相同的 sn-p

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ....
        if ([cell isOrange]) {
            cell.nameLabel.textColor = RabatOrangeColor;
        }
        else{
            cell.nameLabel.textColor = RabatGrayColor;
        }
     }
    

    【讨论】:

      最近更新 更多