GithubHelp home page GithubHelp logo

lamar8 / easytables Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcoconti83/easytables

0.0 1.0 0.0 451 KB

Easy to set up array-based NSTableView for Cocoa

License: MIT License

Objective-C 3.20% Swift 96.80%

easytables's Introduction

EasyTables

EasyTables allows for an easy way of programmatically creating and setting up a NSTableView to display textual information backed by a collection of elements. It also allows for easy creation of contextual menus (right-click "pop-up" menu) and to easily react on selection, without using interface builder. The columns are also sortable by string comparison on the displayed string.

How to use

It's as simple as defining the list of colums, what operations should appear in the contextual menu, and how to react to selection.

Here's an example, that uses no interface builder and no XIB to programmatically create a NSTableView, populate its values and define some operations:

import Cocoa
import EasyTables

class ViewController: NSViewController {

    var configuration: EasyTableSource<String>!
    
    var objects = Set(["Action", "Engineering", "Cod", "Doodle"])
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let (scroll, table) = NSTableView.inScrollView()
        self.view.addSubview(scroll)
        scroll.createConstraintsToFillParent(self.view)
        
        self.configuration = EasyTableSource(
            initialObjects: objects,
            columns: [
                ColumnDefinition("Word", { $0 }),
                ColumnDefinition("Length", { "\($0.characters.count)" }),
            ],
            contextMenuOperations: [
                // Remove object from the table
                ObjectOperation(label: "Remove", action: {
                    [weak self] (items: [String]) -> Void in
                    guard let `self` = self else { return }
                    items.forEach {
                        self.objects.remove($0)
                    }
                    self.configuration.setContent(self.objects) // changing the content automatically updates the table
                }),
                // Uppercase the string(s)
                ObjectOperation(label: "Convert to uppercase", action: {
                    [weak self] (items: [String]) -> Void in
                    guard let `self` = self else { return }
                    items.forEach {
                        self.objects.remove($0)
                        self.objects.insert($0.uppercased())
                    }
                    self.configuration.setContent(self.objects) // changing the content automatically updates the table
                })
            ],
            table: table,
            selectionModel: .multipleNative,
            selectionCallback: {
                /// Just print out something when selected
                print("Selection changed:")
                $0.forEach { print("Selected", $0) }
            })
    }
}

The result of this setup is shown in the following screenshot: Screenshot of table

Other cell values

Tables can display formatted strings (NSAttributedString), controls and images.

Selection models

Tables have 4 selection modes:

  • no selection
  • single row selection
  • multiple row selection
  • use checkboxes for multiple row selection instead of the standard Cocoa selection model of click or Shift/Cmd-click

Screenshot of table

Credits

The images used in the example are from Unsplash.

easytables's People

Contributors

marcoconti83 avatar lamar8 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.