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

时间:2011-02-12 16:46:42

标签: xcode ios uitableview uilabel

我在更改UILabel textColor属性方面遇到一个小问题,它在tableView didSelectRowAtIndexPath方法中没有改变颜色我正在使用SubClassed的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确实正确更改,因此我正在访问单元格

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用此:

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