Ответ 1
Попробуйте следующее:
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()
}
}
Примечание. Изменение цвета изображения tintColor в соответствии с вашим требованием.