You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除NSScrollView中NSTableView的左右边框?

问题描述

通过代码在NSScrollView中创建NSTableView,功能正常但表格左右两侧存在边框。修改ContextView中表格的frame、设置makeNSView函数中的列宽均无法移除该边框,实现代码及效果如下:

实现代码

// in context view:
BetterTableView()
  .frame(width: 350, height: 150, alignment: .center)

// better table view and associated data
struct ClosingValue: Identifiable {
    var id: UUID
    var name: String
    var date: String
    var close: Float
    
    static var closingValues = [
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 01, 2009", close: 1.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 02, 2009", close: 2.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 03, 2009", close: 3.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 04, 2009", close: 4.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 05, 2009", close: 5.10),
        ClosingValue(id: UUID(), name: "Stock1", date: "Nov 06, 2009", close: 6.10)
    ]
}

struct BetterTableView: NSViewRepresentable {
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource {
        
        @State var closingValues: [ClosingValue] = ClosingValue.closingValues
        
        func numberOfRows(in tableView: NSTableView) -> Int {
            closingValues.count
        }
        
        func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
            
            var nsView = NSView()
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .center
            
            let attributes1: [NSAttributedString.Key: Any] = [
                .foregroundColor: NSColor.blue,
                .paragraphStyle: paragraphStyle,
                .font: NSFont(name: "Arial", size: 14.0) as Any
            ]
            
            var attributedString = NSAttributedString()
            let tempNSView = NSTextField()
            
            switch tableColumn {
                
            case tableView.tableColumns[0]:
                attributedString = NSAttributedString(string: closingValues[row].name, attributes: attributes1)
                
            case tableView.tableColumns[1]:
                attributedString = NSAttributedString(string: closingValues[row].date, attributes: attributes1)
                
            case tableView.tableColumns[2]:
                let closeAsString = String(format: "$%.2f", closingValues[row].close)
                attributedString = NSAttributedString(string: closeAsString, attributes: attributes1)
                
            default:
                print("problem in table view switch statement")
            }
            
            tempNSView.backgroundColor = NSColor.white
            tempNSView.isBordered = true
            tempNSView.isEditable = false
            tempNSView.attributedStringValue = attributedString
            nsView = tempNSView
            
            return nsView
        }
    } // end of coordinator class
    
    func makeNSView(context: Context) -> NSScrollView {
        
        let tableView = NSTableView()
        
        tableView.delegate = context.coordinator
        tableView.dataSource = context.coordinator
        tableView.addTableColumn(NSTableColumn())
        tableView.addTableColumn(NSTableColumn())
        tableView.addTableColumn(NSTableColumn())
        tableView.intercellSpacing = NSSize(width: 0.0, height: 0.0)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: NSColor.black,
            .paragraphStyle: paragraphStyle,
            .font: NSFont.systemFont(ofSize: 16)
        ]
        
        let column0AttributedString = NSAttributedString(string: "Stock", attributes: attributes)
        let column0Header = tableView.tableColumns[0]
        column0Header.headerCell.drawsBackground = true
        column0Header.headerCell.backgroundColor = NSColor.systemMint
        column0Header.headerCell.alignment = .center
        column0Header.headerCell.attributedStringValue = column0AttributedString
        column0Header.sizeToFit()
        column0Header.width = 90.0
        
        let column1AttributedString = NSAttributedString(string: "Date", attributes: attributes)
        let column1Header = tableView.tableColumns[1]
        column1Header.headerCell.drawsBackground = true
        column1Header.headerCell.backgroundColor = NSColor.systemMint
        column1Header.headerCell.alignment = .center
        column1Header.headerCell.attributedStringValue = column1AttributedString
        column1Header.sizeToFit()
        column1Header.width = 130.0
        
        let column2AttributedString = NSAttributedString(string: "Closing", attributes: attributes)
        let column2Header = tableView.tableColumns[2]
        column2Header.headerCell.drawsBackground = true
        column2Header.headerCell.backgroundColor = NSColor.systemMint
        column2Header.headerCell.alignment = .center
        column2Header.headerCell.attributedStringValue = column2AttributedString
        column2Header.sizeToFit()
        column2Header.width = 70.0
        
        let scrollView = NSScrollView()
        scrollView.documentView = tableView
        
        return scrollView
    }
    
    func updateNSView(_ nsView: NSScrollView, context: Context) {
        let tableView = (nsView.documentView as! NSTableView)
        // work on this section
    }
}

效果截图

表格显示效果截图

解决方案

左右边框/留白问题源于NSTableView和NSScrollView的默认边框,加上列宽总和小于滚动视图宽度导致的留白,可通过以下步骤解决:

  • 关闭默认边框:在makeNSView函数中,给表格和滚动视图添加边框关闭设置:

    tableView.borderType = .noBorder
    scrollView.borderType = .noBorder
    
  • 让表格宽度自适应滚动视图:设置表格的自动调整掩码,避免宽度不匹配产生留白:

    tableView.autoresizingMask = [.width]
    
  • 匹配列宽总和与滚动视图宽度:当前列宽总和为290,而滚动视图宽度是350,可调整列宽让总和对齐,比如修改第二列宽度:

    column1Header.width = 190.0 // 90+190+70=350
    
  • (可选)关闭单元格边框:若不需要单元格的边框,可在创建tempNSView时设置:

    tempNSView.isBordered = false
    

修改后的makeNSView核心代码示例:

func makeNSView(context: Context) -> NSScrollView {
    
    let tableView = NSTableView()
    tableView.delegate = context.coordinator
    tableView.dataSource = context.coordinator
    tableView.addTableColumn(NSTableColumn())
    tableView.addTableColumn(NSTableColumn())
    tableView.addTableColumn(NSTableColumn())
    tableView.intercellSpacing = NSSize(width: 0.0, height: 0.0)
    
    // 新增:关闭表格边框+自适应宽度
    tableView.borderType = .noBorder
    tableView.autoresizingMask = [.width]
    
    // ... 原有列配置代码 ...
    
    // 调整列宽总和匹配滚动视图宽度
    column0Header.width = 90.0
    column1Header.width = 190.0
    column2Header.width = 70.0
    
    let scrollView = NSScrollView()
    scrollView.documentView = tableView
    // 新增:关闭滚动视图边框
    scrollView.borderType = .noBorder
    
    return scrollView
}

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 06:12:33