【问题标题】:UITableViewCell selectedBackground shadow not displayed on deviceUITableViewCell selectedBackground 阴影未显示在设备上
【发布时间】:2014-06-28 03:52:13
【问题描述】:

这个被我扔了,我想知道是否有人可以帮忙。我正在尝试在我选择的 UITableViewCell 上显示阴影,它在模拟器上运行良好,但在运行 iOS 7.0.4 的 iPad 3rd Gen 上却不行

我有一个子类 UITableViewCell,它正在分配/初始化一个子类 UIView 作为其 selectedBackground。除了没有显示在设备上之外,一切正常。

在我的子类 UITableViewCell...

- (void)awakeFromNib
{
    self.selectedBackgroundView = [[OTHD_CellSelectedView alloc] initWithFrame:self.bounds];

}

在我的 OTHD_CellSelectedView...

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createLayer];
    }
    return self;
}

- (void) createLayer
{
    CALayer *the_layer = [CALayer layer];
    [self.layer insertSublayer:the_layer atIndex:0];

    the_layer.frame = self.bounds;
    the_layer.backgroundColor = [[UIColor whiteColor] CGColor];
    the_layer.shadowColor = [UIColor blackColor].CGColor;
    the_layer.shadowOffset = CGSizeMake(0, 0);
    the_layer.shadowRadius = 10.0;
    the_layer.shadowOpacity = 0.7;
}

【问题讨论】:

  • 作为实验,尝试将 CALayer *the_layer 声明为属性,并使其成为强引用。有什么改变吗?
  • 您在其他设备上尝试过吗?可能是视网膜第三代 iPad 上的图形无法支持某些视觉效果。模拟器通常是 UI 效率和支持的危险指南,因为它将使用 Mac 的全部资源来完成它。
  • @Tander,感谢您的建议,但不,这不会改变任何事情。
  • @Cocoadelica,这可能会在另一台设备上工作,但它对 CALayer 的使用相当少,我确实需要支持所有运行 iOS7 的 iPad。
  • 你在后来的设备上试过了吗? iPhone 4S / iPhone 5s?

标签: objective-c ipad ios7 uitableview calayer


【解决方案1】:

我认为您应该将表格中的单元格带到前面我已经尝试过,它对我有用...希望这对您有所帮助:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Cell"];
    }

    if (indexPath.row == selected)
    {
        cell.layer.backgroundColor = [[UIColor whiteColor] CGColor];
        cell.layer.shadowColor = [UIColor blackColor].CGColor;
        cell.layer.shadowOffset = CGSizeMake(0, 0);
        cell.layer.shadowRadius = 10.0;
        cell.layer.shadowOpacity = 0.7;

        [cell.superview bringSubviewToFront:cell];
    }
    else
    {
        cell.layer.shadowOpacity = 0;
    }


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selected = indexPath.row;
    [tableView reloadData];

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
}

【讨论】:

  • 感谢 Rajesh 的建议,这是更改单元格的层,而不是单元格的 selectedBackgroundView.layer,但如果这是我需要做的让它工作,那就这样吧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多