2017-08-28 78 views
19

我的应用程序具有与图像和文本字段一个的tableview:的tableview图像内容选择颜色

  • 图像=图像渲染为模板图像(浅灰色)
  • 文本字段=文本颜色黑

如果我选择一排,两者的颜色会完全变成白色

的问题 - 我的形象改变为蓝色图像渲染=默认。 如果我现在选择一行,我的文本字段的文本颜色将变为白色,但图像将保持蓝色。

我想要的图像颜色改为白色太多,但事实并非如此。

我做错了什么?

实施例与图像渲染为模板模式=>默认:灰色|选择自动白色

enter image description here

实施例与彩色图像渲染为默认模式=>默认:绿色|也选绿色|预计白色的,但它留下的绿色

enter image description here

+0

是不是这同样的问题?分别相同的解决方案? https://stackoverflow.com/questions/33249456/nstableviewcell-setselected –

+0

我想这是不一样的情况,是不是? – Ghost108

+0

我不是在MacOS上的专家,但我的猜测是建立NSTableCellView,并通过自己的高亮或其他相关行动didSet手柄选择VAR自定义子类。 –

回答

3

试试这个:

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颜色按您的要求。

table row selection with color
希望它会帮助你。

+0

我需要一个osx解决方案:( – Ghost108

+0

@ Ghost108根据osx解决方案更新了我的代码 – Yuyutsu

+0

令人难以置信的是,osx解决方案如此复杂,在iOS中更容易:/ – Ghost108

2

创建一个自定义单元格和覆盖setHighlighted(_ highlighted: Bool, animated: Bool)改变iamgeView的tintColor:

override func setHighlighted(_ highlighted: Bool, animated: Bool) { 
    super.setHighlighted(highlighted, animated: animated) 
    imageView?.tintColor = highlighted ? UIColor.white : UIColor.green 
} 

然后,当你创建你的,你有UIImageRenderingMode.alwaysTemplate设置图像:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = CustomCell() 
    cell.imageView?.image = UIImage(named: "custom_image")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate) 
    cell.imageView?.tintColor = UIColor.green 
    return cell 
} 
+0

并再次:我需要一个osx解决方案 – Ghost108