无法删除 NSScrollView 中 NSTableview 的左右边框

问题描述 投票:0回答:1

我有在 NSScrollView 中创建 NSTableView 的代码。它有效,但我最终在显示的表格的左侧和右侧有边框。 ContextView 中的表调用有一个框架,但更改它不会删除边框。我在 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
    }
}

xcode macos swiftui nstableview nsscrollview
1个回答
0
投票

设置

tableView.style = .plain
这是文档。

© www.soinside.com 2019 - 2024. All rights reserved.