为什么我的detailTextLabel会移动?

时间:2013-03-02 16:22:14

标签: ios uitableview

我在UITableViewCell的子类中使用以下代码为UITableView

中未选中的单元格设置投影
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
  [super setHighlighted:highlighted animated:animated];
  [self applyLabelDropShadow:!highlighted];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];
  [self applyLabelDropShadow:!selected];
}

- (void)applyLabelDropShadow:(BOOL)applyDropShadow
{
  self.textLabel.shadowColor = applyDropShadow ? [UIColor whiteColor] : nil;
  self.textLabel.shadowOffset = CGSizeMake(0, 1);
  self.detailTextLabel.shadowColor = applyDropShadow ? [UIColor whiteColor] : nil;
  self.detailTextLabel.shadowOffset = CGSizeMake(0, 1);
}

这个代码来自另一个StackOverflow question Mike Stead,而且效果很好。

当行从选定位置移动到取消选择时,您可以看到detailTextLabel向下轻微移位,我不想发生这种情况。对于单元格,textLabel不会发生这种情况。

任何想法为什么?

1 个答案:

答案 0 :(得分:2)

尝试将[UIColor clearColor]用于非阴影而不是nil

- (void)applyLabelDropShadow:(BOOL)applyDropShadow
{
  self.textLabel.shadowColor = applyDropShadow ? [UIColor whiteColor] : [UIColor clearColor];
  self.textLabel.shadowOffset = CGSizeMake(0, 1);
  self.detailTextLabel.shadowColor = applyDropShadow ? [UIColor whiteColor] : [UIColor clearColor];
  self.detailTextLabel.shadowOffset = CGSizeMake(0,1);
}