简体   繁体   English

在自定义UITableViewCell中更改UILabel的颜色

[英]change color of UILabel in custom UITableViewCell

I am having a small issue with changing of a UILabel textColor property, it does not change color within the tableView didSelectRowAtIndexPath method I am using a customCell that is SubClassed and 我在更改UILabel textColor属性时遇到一个小问题,它不会更改tableView didSelectRowAtIndexPath方法中的颜色我正在使用SubClassed和

I am using the latest XCode4 GM version and was wondering if anyone else is experiencing anything similar or perhaps its a bug in the version of xcode 我正在使用最新的XCode4 GM版本,并且想知道是否有人遇到类似的东西,或者可能是xcode版本中的错误

UPDATE: 更新:

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

I would like to add that I have tried in other methods to achieve the result but with no luck, I am going to now try the willDisplayCell method and see how that goes, thanks for the help. 我想补充一点,我已经尝试了其他方法来获得结果,但是没有运气,我现在将尝试willDisplayCell方法,看看效果如何,感谢您的帮助。

Also the backgroundView does change properly so I am accessing the cell 而且backgroundView确实正确更改,所以我正在访问单元格

Use this : 用这个 :

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM