Skip to content

Instantly share code, notes, and snippets.

@DivineDominion
Created May 18, 2016 17:17
Show Gist options
  • Save DivineDominion/c069c8d3d38d7cb880b5354524acf579 to your computer and use it in GitHub Desktop.
Save DivineDominion/c069c8d3d38d7cb880b5354524acf579 to your computer and use it in GitHub Desktop.
WTF: Crashing NSTableHeaderCell subclass ¯\_(ツ)_/¯
//
// AppDelegate.swift
// TableZombies
//
// Created by Christian Tietze on 18/05/16.
// Copyright © 2016 Christian Tietze. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet var tableVC: TableViewController!
}
let first = ["1", "2"]
let second = ["a", "b", "c", "d", "e", "f", "g"]
class TableViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
var model = 1
@IBOutlet var button: NSButton!
var tableView: NSTableView! { return self.view as? NSTableView }
var tableDidLoad = false
override func awakeFromNib() {
super.awakeFromNib()
guard !tableDidLoad else { return }
tableDidLoad = true
removeAllColumns()
switchTable(self)
}
func removeAllColumns() {
tableView.tableColumns.forEach(tableView.removeTableColumn)
}
@IBAction func switchTable(sender: AnyObject) {
self.model = model == 1 ? 2 : 1
replaceTable()
}
func replaceTable() {
let currentModel = [first, second][model-1]
removeAllColumns()
let content = "anything"
let columns: [NSTableColumn] = currentModel.enumerate().map { index, columnContent in
let column = NSTableColumn()
let headerCell = TableHeaderCell()
headerCell.content = "\(content)" // <-- this breaks it! Replace it with const value and everything's fine o_O
column.headerCell = headerCell
column.title = columnContent
return column
}
columns.forEach(tableView.addTableColumn)
}
}
class TableHeaderCell: NSTableHeaderCell {
var content: String!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment