2017-08-28 76 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