【问题标题】:tableview image content selection colortableview 图像内容选择颜色
【发布时间】:2017-08-28 15:40:06
【问题描述】:

我的应用有一个带有图像和文本字段的表格视图:

  • 图像 = 图像呈现为模板图像(浅灰色)
  • textfield = 文本颜色黑色

如果我选择一行,两者的颜色都会完全变为白色

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

我希望图像也将颜色更改为白色,但它没有。

我做错了什么?

将图像渲染为模板模式的示例 => 默认值:灰色 |自动选择白色

以彩色图像渲染为默认模式的示例 => 默认:绿色 |还选了绿色|预期为白色,但它保持绿色

【问题讨论】:

  • 这不是同一个问题吗?分别相同的解决方案? stackoverflow.com/questions/33249456/…
  • 我猜这不是同一种情况,不是吗?
  • 我不是 macOS 方面的专家,但我的猜测是创建 NSTableCellView 的自定义子类,并在您自己的高亮或其他相关操作的 didSet 句柄中选择 var。
  • 这是正确的方法吗?我不敢相信这这么复杂
  • 我真的很想说是或否,但我不知道。这只是某种方式,如何做到这一点。当您弄清楚时,如果正确与否,请在此处写下。我真的很好奇。

标签: swift macos cocoa colors tableview


【解决方案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()
    }
}

注意:根据您的要求更改 imageView tintColor 颜色。


希望对你有所帮助。

【讨论】:

  • 我需要一个 osx 解决方案 :(
  • 难以置信 osx 解决方案如此复杂。在 iOS 中它更容易:/
  • 你试过了吗?让我知道它是否有效。
  • 是的,我几天前试过这个。我在堆栈溢出时也找到了这个解决方案。但我不敢相信这必须如此复杂。我猜你的代码tableViewSelectionDidChange 不起作用。我试过这个,如果我把我的outlineView.reloadData() 放在这个函数中,选择总是会通过调用 reloadData => 循环来改变:(
  • @Ghost108 不要调用reloadData,查看文档以获取重新加载一个单元格的方法。
【解决方案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
}

【讨论】:

  • 再说一遍:我需要一个 osx 解决方案
【解决方案3】:

迟到总比没有好。

这是我的一个朋友帮助我的解决方案。实现自定义NSImageCell 类并覆盖interiorBackgroundStyle

#import <Cocoa/Cocoa.h>

NS_ASSUME_NONNULL_BEGIN

@interface TFSelectStateImageCell : NSImageCell

@end

NS_ASSUME_NONNULL_END
#import "TFSelectStateImageCell.h"

@implementation TFSelectStateImageCell


- (NSBackgroundStyle)interiorBackgroundStyle {
    NSBackgroundStyle style = [super interiorBackgroundStyle];
    self.image.template = (style == NSBackgroundStyleEmphasized);
    return style;
}

@end

通过这种方式,您可以在选择行时获得 NSImagetemplate 属性的好处,但在未选择行时,您将获得全彩色图像。

【讨论】:

    猜你喜欢
    • 2011-04-21
    • 1970-01-01
    • 2013-07-31
    • 2023-03-18
    • 1970-01-01
    • 2015-05-08
    • 2017-07-08
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多