我的应用程序有一个带有图像和文本字段的表格视图:
- image = 图像渲染为模板图像(浅灰色)
- 文本字段 = 文本颜色黑色
如果我选择一行,两者的颜色都会完美地变为白色
问题 - 我将图像更改为蓝色图像 = 默认渲染。
如果我现在选择一行,文本字段的文本颜色将更改为白色,但图像将保持蓝色。
我希望图像也将颜色更改为白色,但事实并非如此。
我做错了什么?
图像渲染为模板模式的示例 => 默认:灰色 |自动选择白色
以彩色图像渲染为默认模式的示例 => 默认:绿色 |选的还绿|预期是白色,但它保持绿色
尝试这个:
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 颜色。
Hope it will help you.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)