tableview图像内容选择颜色 [英] tableview image content selection color
问题描述
我的应用程序具有带有图像和文本字段的表格视图:
My app has a tableview with an image and a textfield:
- image =将图像渲染为模板图像(浅灰色)
- textfield =黑色文本
如果我选择一行,则两者的颜色都会完美地变为白色
If I select a row, the color of both will change perfectly to white
问题-我将图像更改为蓝色图像=默认渲染. 如果现在选择一行,我的文本字段的文本颜色将变为白色,但图像将保持蓝色.
Question - I change the image to a blue color image = render as default. If I now select a row, the text color of my textfield will change to white, but the image will stay blue.
我也希望图像也将颜色更改为白色,但不要这样做.
I want the image change the color to white too but it doesn't.
我做错了什么?
将图像渲染为模板模式的示例=>默认值:灰色|自动选择白色
以彩色图像作为默认模式渲染的示例=>默认值:绿色|选择还绿色|预期为白色,但仍为绿色
推荐答案
尝试一下:
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var tableView:NSTableView!
var selectIndex = -1
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tintedImage(_ image: NSImage, tint: NSColor) -> NSImage {
guard let tinted = image.copy() as? NSImage else { return image }
tinted.lockFocus()
tint.set()
let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
NSRectFillUsingOperation(imageRect, .sourceAtop)
tinted.unlockFocus()
return tinted
}
}
extension ViewController:NSTableViewDataSource, NSTableViewDelegate{
func numberOfRows(in tableView: NSTableView) -> Int {
return 3
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?{
let result = tableView.make(withIdentifier: "imageIcon", owner: self) as! NSTableCellView
if selectIndex == row{
result.imageView?.image = self.tintedImage(NSImage(named:"file")!, tint: NSColor.green)
}else{
result.imageView?.image = self.tintedImage(NSImage(named:"file")!, tint: NSColor.gray)
}
return result
}
func tableView(_ tableView: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int) {
if selectIndex == row{
rowView.backgroundColor = NSColor.blue
}else{
rowView.backgroundColor = NSColor.clear
}
}
func tableViewSelectionDidChange(_ notification: Notification) {
let table = notification.object as! NSTableView
self.selectIndex = tableView.selectedRow
print(table.selectedRow);
table.reloadData()
}
}
注意:根据您的要求更改imageView tintColor的颜色.
Note: Change imageView tintColor color as per your requirement.
希望它会对您有所帮助.
Hope it will help you.
这篇关于tableview图像内容选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!