简体   繁体   English

双击可可时如何自动调整表格列的大小?

[英]How to auto resize table column when double clicking on Cocoa?

I want to auto resize table column to its content width when double clicking its header (resize cursor is showing), like on iTunes.我想在双击表头(显示调整大小光标)时将表列自动调整为其内容宽度,就像在 iTunes 上一样。

Anyone know how to do it ?有人知道怎么做吗?

Thanks in advance提前致谢

Look at the NSTableView method setDoubleAction: .查看NSTableView方法setDoubleAction: You can probably set it up from Interface Builder, too - I didn't check.您也可以从 Interface Builder 进行设置 - 我没有检查。 From the documentation :文档中:

setDoubleAction:设置双重动作:

Sets the message sent to the target when the user double-clicks an uneditable cell or a column header to a given selector.设置当用户双击给定选择器的不可编辑单元格或列标题时发送到目标的消息。

 - (void)setDoubleAction:(SEL)aSelector

Parameters参数
aSelector选择器
The message the receiver sends to its target when the user double-clicks an uneditable cell or a column header.当用户双击不可编辑的单元格或列标题时接收器发送到其目标的消息。

Discussion讨论
If the double-clicked cell is editable, this message isn't sent and the cell is edited instead.如果双击的单元格是可编辑的,则不会发送此消息,而是编辑单元格。 You can use this method to implement features such as sorting records according to the column that was double-clicked.您可以使用此方法实现根据双击的列对记录进行排序等功能。 See also clickedRow which you can use to determine if a row was clicked rather than the column heading.另请参阅clickedRow ,您可以使用它来确定是否单击了行而不是列标题。

For the method to have any effect, the receiver's action and target must be set to the class in which the selector is declared.要使方法生效,必须将接收者的动作和目标设置为声明选择器的类。 See Action Messages for additional information on action messages.有关操作消息的更多信息,请参阅操作消息。

If you're using a view-based table view and targeting macOS 10.6 or above, you can implement the NSTableViewDelegate method tableView(_:sizeToFitWidthOfColumn:) .如果您使用基于视图的表格视图并针对 macOS 10.6 或更高版本,则可以实现 NSTableViewDelegate 方法tableView(_:sizeToFitWidthOfColumn:)

Here is a simple Swift implementation I did which proved to be fast enough for a table with 10 columns and a few hundred rows:这是我做的一个简单的 Swift 实现,它被证明对于一个有 10 列和几百行的表来说足够快:

func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
    var longestWidth = 0.0
    var text: String

    for row in 0..<tableView.numberOfRows {
        let item = myDataSource[row]

        switch column {
            case 0: text = item.firstName
            case 1: text = item.middleName
            case 2: text = item.lastName
            case 3: text = item.streetNumber
            case 4: text = item.city
            case 5: text = item.region
            case 6: text = item.country
            case 7: text = item.bloodType
            case 8: text = item.gender
            case 9: text = item.nationality
            default: text = ""
        }

        let textSize = text.size(withAttributes: [
            // .regular requires macOS 10.12+
            .font: NSFont.systemFont(ofSize: NSFont.systemFontSize(for: .regular))
        ])

        if textSize.width > longestWidth {
            longestWidth = textSize.width
        }
    }

    return longestWidth + 8 // some padding
}

If it becomes slow, you probably should calculate the size of the visible rows and a dozen more above and bellow or find a better implementation.如果它变慢了,你可能应该计算可见行的大小以及更多的上面和下面的行,或者找到一个更好的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM