为什么我的自定义单元格仅在用户移开手指时突出显示?

发布于 2024-11-02 04:37:03 字数 546 浏览 3 评论 0原文

我创建了一个自定义 UITableViewCell,并使用以下代码设置背景视图:

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NormalImage"]];
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HighlightedImage"]];

    // other layout stuff
}

这工作正常,除了当用户选择一行时,它不会突出显示 - 它仅在用户抬起时突出显示手指(这样在转换到下一个视图之前您会短暂地看到它突出显示)。如果我删除 self.selectedBackgroundView 行,以便有正常的 iOS 蓝色单元格突出显示,那么一旦选择该单元格,它就会突出显示。

这是为什么呢?

I have created a custom UITableViewCell, and set the backgroundView with the following code:

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NormalImage"]];
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HighlightedImage"]];

    // other layout stuff
}

This works fine except that, when the user selects a row, it does not highlight - it only highlights when the user lifts off their finger (so you briefly see it highlight before transitioning to the next view). If I remove the self.selectedBackgroundView line, so that there is the normal iOS blue cell highlight, it will highlight as soon as the cell is selected.

Why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-11-09 04:37:03

在自定义表格单元格中尝试此代码

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HighlightedImage"]];

    } else {
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NormalImage"]];

    }
}

Try this code in the custom table cell

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HighlightedImage"]];

    } else {
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NormalImage"]];

    }
}
九局 2024-11-09 04:37:03

您可以在初始化自定义单元格时分配backgroundView和selectedBackgroundView。根据 apple的doc,selectedBackgroudView作为单元格选中时的背景。

如果您将这些代码编写在 -(void)layoutSubviews 中,则当选择或取消选择单元格时,它将调用 -(void)layoutSubviews。然后,每次自定义单元格的状态发生更改时,您的backgroundView和selectedBackgroundview将被重新分配给新的UIView实例。

You can assign backgroundView and selectedBackgroundView when initializing your custom cell. According to apple's doc, the selectedBackgroudView is used as the background when the cell is selected.

If you write those codes in -(void)layoutSubviews, when the cell is selected, or unselected, it will call -(void)layoutSubviews. Then, your backgroundView and selectedBackgroundview will be re-assigned to new UIView instances each time when the state of custom cell has been changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文