【问题标题】:change color of UILabel in custom UITableViewCell在自定义 UITableViewCell 中更改 UILabel 的颜色
【发布时间】:2011-02-12 16:46:41
【问题描述】:

我在更改 UILabel textColor 属性时遇到了一个小问题,它不会在 tableView didSelectRowAtIndexPath 方法中改变颜色我使用的是子类化的 customCell 和

我正在使用最新的 XCode4 GM 版本,想知道是否有其他人遇到过类似的情况 或者它可能是 xcode 版本中的错误

更新:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  NSIndexPath *indexPath = [tableView indexPathForCell:(HSCustomCellTF *)[[textField superview] superview]];
  HSCustomCellTF *cell = (HSCustomCellTF *)[tableView cellForRowAtIndexPath:indexPath];
  cell.keyLBL.textColor = [UIColor redColor];
  cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"middleRowSelectedBG.png"]] autorelease];

  tableView.userInteractionEnabled = NO;

  return YES;
}

我想补充一点,我已经尝试过其他方法来达到结果,但没有运气, 我现在要试试willDisplayCell 方法,看看效果如何,谢谢你的帮助。

backgroundView 也正确更改,所以我正在访问单元格

【问题讨论】:

  • 您能提供您尝试使用的代码吗?

标签: xcode ios uitableview uilabel


【解决方案1】:

【讨论】:

  • 这不起作用,我想因为我没有编辑表格,我有一个 uilabel 和 uitextfield 并且 TF 是隐藏的,我使用 shouldChangeCharactersInRange 方法来更新标签,我使用它是因为我不希望用户输入小数,值是价格
  • 好吧,经过一番调查,你是正确的,这是这样做的方法,但它仍然没有更新标签颜色,任何想法
【解决方案2】:

好的,我找到了答案,似乎当您使用自定义单元格时,并且想要编辑标签颜色,然后在 tableViewCell 的子类中,不要在 layoutSubviews 方法中设置属性,一次我从该方法中删除了 textColor 属性@gkchristopher 答案是正确的,谢谢大家的帮助,希望这对其他人有帮助。

【讨论】:

    【解决方案3】:

    使用这个:

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
    
        cell.label.textColor = [UIColor whiteColor];
    
        return indexPath;
    }
    - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
    
        cell.label.textColor = [UIColor darkGrayColor];
    
        return indexPath;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多