更改选择时的 NSTextFieldCell 背景颜色
我试图在选择单元格时更改 NSTextFieldCell 的背景颜色。
这是代码:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[super drawWithFrame:cellFrame inView:controlView];
if([self isHighlighted]) {
[self setBackgroundColor:[NSColor whiteColor]];
}
}
但所选行始终是蓝色的。我错过了什么?
注意:这不是 iOS 应用程序。
预先感谢您。
I'm trying to change the background color of a NSTextFieldCell when the cell is selected.
This is the code:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[super drawWithFrame:cellFrame inView:controlView];
if([self isHighlighted]) {
[self setBackgroundColor:[NSColor whiteColor]];
}
}
But the selected row is always blue. I am missing something?
Note: this is not an iOS application.
Thanks you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不像我之前想象的那么容易:
NStableview 那里有一些问题。如果你使用类似的东西:
你必须这样做,
如果你的单元格不是自定义的,这就足够了。
如果你改变高度和内部,它必须更复杂:
保留选择样式表供你选择。
在细胞子类中:
你问我为什么改变填充的大小?
因为当你使用背景时,苹果会留下一个小盒子,它会有不同的颜色。
This is not easy as i think before:
NStableview have some problem there. If u using something like:
You have to do
This is enough if u cell is not custom.
If u change height and interior, it have to be more complicated:
keep selection style table for you choice.
In cell subclass:
You ask me why i change size of filling?
Bcs when u using background, apple leave a small box, which will have different color.
不要那样做。相反,当您在 -tableView:cellForRowAtIndexPath: 中创建单元格时,请将单元格的“selectedBackgroundView”属性设置为在选择单元格时将成为背景的视图。这可以只是一个带有背景颜色的普通旧 UIView。我在 UIView 上创建了一个类别,它有一个名为 +backgroundViewForTableCell: 的方法,它正是这样做的......实例化一个视图并将其背景颜色设置为我想要的颜色。创建单元格时,我会这样使用它:
点击单元格时您可能想要做的另一件事是设置单元格中任何文本的颜色。例如,如果背景颜色相对较暗,您可能需要将文本颜色从黑色更改为白色。
Don't do it that way. Instead, when you create the cell in your -tableView:cellForRowAtIndexPath:, set the cell's "selectedBackgroundView" property to a view that will become the background when the cell is selected. This can be just a plain old UIView with your background color. I created a category on UIView that has a method called +backgroundViewForTableCell:, which does exactly that... instantiates a view and sets its background color to the color I want. I use it like this when I create the cell:
The other thing you might want to do when the cell is tapped is to set the color of any text in the cell. If your background color is relatively dark you might want to change the text color from black to white, for example.