【问题标题】:tableView(tableView, viewFor:,: Int) strange behaviortableView(tableView, viewFor:,: Int) 奇怪的行为
【发布时间】:2019-09-15 01:01:24
【问题描述】:

我正在尝试制作一个基于视图的 tableView 委托方法来更改列的字体 IFF 它的标识符是“名称”并且它的值与某个全局匹配;否则以笔尖为准。我认为我有一个新手设置问题,或者我的逻辑忽略了某些内容。

我有两个表,一个基于视图 (tag=0),一个单元格 (tag=1),并且它们都具有相同的视图控制器作为它们的委托。每个 tableView 都有一个唯一的标签和标识符,一个用于列值绑定的数组控制器,并且每个适合列都有一个明确的唯一标识符 - 匹配它的绑定;我曾尝试省略这个 - “自动设置” - 但这没有产生任何数据!?

所以,我添加了这个方法,但是两个 tableView 都调用了它;我只期望第一个。第 2 个表是第 1 个表的详细信息,具有第 1 个阵列控制器选择。

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    let item = ((tableView.dataSource as! NSArrayController).arrangedObjects as! [AnyObject])[row]
    var view = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self)
    if view ==  nil {
        Swift.print("tag: \(tableView.tag) ident: \(tableColumn!.identifier.rawValue)")
        view = NSTableCellView.init()
        view?.identifier = tableColumn?.identifier
    }

    guard tableView.tag == 0 else { return view }

    guard isGlobalPlist, item.name == '<some-global-string>',
        let identifier = tableColumn?.identifier,
        identifier.rawValue == "name" else { return view }

    if let cellView = view as? NSTableCellView {
        if tableView.selectedRowIndexes.intersection(IndexSet.init(integer: row)).count > 0 {
            cellView.textField?.font = .boldSystemFont(ofSize: -1)
            cellView.textField?.textColor = .white
        }
        else
        {
            cellView.textField?.font = .systemFont(ofSize: -1)
            cellView.textField?.textColor = .blue
        }
    }
    return view
}

首先我注意到该方法是为第二个表调用的,而我没想到它是一个基于单元格的 tableView。

无论如何,当 nil 返回时,我确实创建了单元格,文档引用您只需要匹配标识符:

Note that a cell view’s identifier must be the same as its table column’s identifier for bindings to work. If you’re using bindings, it’s recommended that you use the Automatic identifier setting in Interface Builder.

我做的;使用显式输入的列标识符将两个 tableView 的列(视图或单元格)绑定到各自的数组控制器。

无论如何,我的初衷是基于视图的(第一个)tableView:

  • 视图控制器正在控制全局资源(isGlobalPlist 为真)
  • 行的“名称”值与全局值匹配

影响“名称”单元格以不同的颜色(蓝色/黄色 - 或其他颜色)显示,具体取决于是否由于选择突出显示、暗模式等条件而选择了该行。

所以会有 4 种可能性中的 1 种可能性

  1. 选定的行,匹配全局 -> 前:黄色,后:默认
  2. 选定的行,不匹配全局 -> 前:默认,后:默认
  3. 未选择的行,匹配全局 -> 前:蓝色,后:默认
  4. 未选择的行,不匹配全局 -> 前:默认,后:默认

如果目标行符合要求,着色需要根据它包含在 tableView 的选定索引中而有所不同。

该设置似乎会正确影响最初 - 选定的行和匹配值,但随后设置似乎会在导航到的其他行中出现 - 因此选择会发生变化。最多应该有1行的名称列有这种影响。

我无法判断我的逻辑或设置是否错误;我怀疑我可能需要在四处走动时刷新一下?

【问题讨论】:

    标签: macos swift5


    【解决方案1】:

    好吧,我放弃了基于视图的解决方案,但这满足了我的需求;基于细胞的解决方案:

    func tableView(_ tableView: NSTableView, dataCellFor tableColumn: NSTableColumn?, row: Int) -> NSCell? {
        guard let column = tableColumn else { return nil }
    
        let item : AnyObject = ([table0ArrayController,table1ArrayController][tableView.tag]?.arrangedObjects as! [AnyObject])[row]
        let cell : NSTextFieldCell = column.dataCell(forRow: row) as! NSTextFieldCell
        cell.textColor = .textColor
    
        guard tableView.tag == 0, isGlobalPlist, item.name == '<some-global-string>' else { return cell }
    
        if tableView.selectedRowIndexes.intersection(IndexSet.init(integer: row)).count > 0 {
            cell.textColor = .white
        }
        else
        {
            cell.textColor = .blue
        }
    
        return cell
    }
    

    我想用颜色提醒特定行,除非仅为“名称”列选择行。

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      相关资源
      最近更新 更多