为什么我的自定义单元格仅在用户移开手指时突出显示?
我创建了一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在自定义表格单元格中尝试此代码
Try this code in the custom table cell
您可以在初始化自定义单元格时分配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.