Replacing row view of NStableView on selecting a row - macos

I have a NsTableView and I want to use two type NSTableRowView for rows of table. For selected row There is complex NStableRowView and for non selected row there will be simple NSTableRowView with two labels.
Here is what I have tried --
extension CMMAccountSettingViewController:NSTableViewDelegate{
func tableView(tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
if row == accountListTableView.selectedRow{
var storyboard = NSStoryboard(name: "Settings", bundle: nil)
var accountController = storyboard?.instantiateControllerWithIdentifier("account") as! CMMAccountViewController
var rowView = accountController.view as! CMMAccountView
return rowView
}
else
{
var rowView = accountListTableView.makeViewWithIdentifier("accountSeetingsRow", owner: self) as! CMMAccountRowView
rowView.titleLable.stringValue = listOfAccounts[row].nickname!
rowView.captionLable.stringValue = listOfAccounts[row].accountDisplayName!
return rowView
}
}
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
if accountListTableView.selectedRow == row{
return 350
}
else{
return 75
}
}
func tableViewSelectionDidChange(notification: NSNotification) {
var coloumnIndexSet = NSIndexSet(indexesInRange: NSMakeRange(0, accountListTableView.tableColumns.count))
accountListTableView.reloadDataForRowIndexes(accountListTableView.selectedRowIndexes, columnIndexes: coloumnIndexSet)
}
}
But This approach is not working. How can I replace selected row view with animation?
Thanks for your help.

Related

Swift 3 Table Column Width Last Column Not Working

Refer to my attached image.
Notice the last column for some reason is always short on the width. I can't for the life of me figure out why or how to fix this?
Here is my code for my controller.
import Cocoa
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
#IBOutlet weak var theTableview: NSTableView!
var data:NSArray = [""] //#JA - This is used
override func viewDidLoad() {
super.viewDidLoad()
//First remove all columns
let columns = self.theTableview.tableColumns
columns.forEach {
self.theTableview.removeTableColumn($0)
}
//self.theTableview?.columnAutoresizingStyle = .sequentialColumnAutoresizingStyle
for index in 0...100 {
let column = NSTableColumn(identifier: "defaultheader")
if(index != 0){
column.title = "Month \(index)"
}else{
column.title = "Factors"
}
self.theTableview.addTableColumn(column)
}
// Do any additional setup after loading the view.
data = ["Group 1","Group 2","Group 3","Group 4"]
self.theTableview.reloadData()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func numberOfRows(in tableView: NSTableView) -> Int {
return data.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let cell = tableView.make(withIdentifier: "defaultcell", owner: nil) as? NSTableCellView {
cell.textField?.stringValue = data.object(at: row) as! String
return cell
}
return nil
}
#IBAction func startsimulation(_ sender: NSButton) {
//Recalculates the data variable for updating the table.
data = ["group1","group2"]
theTableview.reloadData()
}
}
NSTableColumn has a property resizingMask and NSTableView has a property columnAutoresizingStyle. Both can be set in IB or in code. Figure out a configuration so the columns behave like you want. The default Column Sizing of the table view in IB is 'Last Column Only', switching to 'None' will fix your problem.
Another solution is setting minWidth of the columns.

NSTableview - Drag and Drop split into two classes and controllers

i found this helpfully tutorial for realize drag an drop with nstabelview:
https://drive.google.com/open?id=0B8PBtMQt9GdONzV3emZGQWUtdmM
this works fine.
but i would like to split both table views into differente view controllers and classes with a split view:
one split view controller:
item 1: viewcontroller with source nstableview (SourceTableView.class)
item 2: viewcontroller with target nstableview (TargetTableView.class)
how can i do this with this project?
i know how can i create a split view controller in storyboard.
but i dont know, if i have two different classes, how the iBoutlet SourceTabelView of class SourceTableView.class assign the iBoutlet TargetTableView of class TargetTableView.class
UPDATE
var person = [Person]()
NSManagedObject.class
import Foundation
import CoreData
#objc(Person)
public class Person: NSManagedObject {
#NSManaged public var firstName: String
#NSManaged public var secondName: String
}
Example of drag and drop between two table views inside a split view. Dragging inside one table view and multiple selection will work. Hold the Option key to drag a copy.
The datasource of each table view is the view controller inside the split view. Each table view has its own view controller and each view controller controls one table view. Both view controllers are the same NSViewController subclass:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
#IBOutlet weak var myTableView: NSTableView!
var dataArray: NSMutableArray = ["John Doe", "Jane Doe", "Mary Jane"]
override func viewDidLoad() {
super.viewDidLoad()
myTableView.register(forDraggedTypes: ["com.yoursite.yourproject.yourstringstype"])
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
// NSTableViewDataSource data methods
func numberOfRows(in tableView: NSTableView) -> Int {
return dataArray.count
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
return dataArray[row] as AnyObject!;
}
// NSTableViewDataSource drag methods
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
// the dragging destination needs the strings of the rows to add to its own data,
// we, the dragging source, need the indexes of the rows to remove the dropped rows.
pboard.declareTypes(["com.yoursite.yourproject.yourstringstype", "com.yoursite.yourproject.yourindexestype"],
owner: nil)
pboard.setData(NSKeyedArchiver.archivedData(withRootObject: (dataArray as NSArray).objects(at:rowIndexes as IndexSet)), forType: "com.yoursite.yourproject.yourstringstype")
pboard.setData(NSKeyedArchiver.archivedData(withRootObject: rowIndexes), forType: "com.yoursite.yourproject.yourindexestype")
return true
}
func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession,
endedAt screenPoint: NSPoint, operation: NSDragOperation) {
// remove the dragged rows if the rows are dragged to the trash or were moved to somewhere else.
var removeRows = false
if operation == .delete {
// trash
removeRows = true
} else if operation == .move {
// check if the point where the rows were dropped is inside our table view.
let windowRect = tableView.convert(tableView.bounds, to: nil)
let screenRect = view.window!.convertToScreen(windowRect)
if !NSPointInRect(screenPoint, screenRect) {
removeRows = true
}
}
if removeRows {
// remove the rows, the indexes are on the pasteboard
let data = session.draggingPasteboard.data(forType: "com.yoursite.yourproject.yourindexestype")!
let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: data) as! NSIndexSet
(dataArray as NSMutableArray).removeObjects(at: rowIndexes as IndexSet)
tableView.reloadData()
}
}
// NSTableViewDataSource drop methods
func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int,
proposedDropOperation dropOperation: NSTableViewDropOperation) -> NSDragOperation {
// only accept drop above rows, not on rows.
if dropOperation == .above {
// return move if the dragging source allows move
if info.draggingSourceOperationMask().contains(.move) {
return .move
}
// return copy if the dragging source allows copy
if info.draggingSourceOperationMask().contains(.copy) {
return .copy
}
}
return []
}
func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int,
dropOperation: NSTableViewDropOperation) -> Bool {
// if the rows were moved inside the same table view we do a reorder
var dropRow = row
if info.draggingSource() as AnyObject === myTableView as AnyObject &&
info.draggingSourceOperationMask().contains(.move) {
// remove the rows from their old position
let data = info.draggingPasteboard().data(forType: "com.yoursite.yourproject.yourindexestype")!
let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: data) as! NSIndexSet
(dataArray as NSMutableArray).removeObjects(at: rowIndexes as IndexSet)
// recalculate the row of the drop
dropRow -= rowIndexes.countOfIndexes(in: NSMakeRange(0, dropRow))
}
// insert the dragged rows
let data = info.draggingPasteboard().data(forType: "com.yoursite.yourproject.yourstringstype")!
let draggedStrings = NSKeyedUnarchiver.unarchiveObject(with: data) as! [Any]
dataArray.insert(draggedStrings, at:IndexSet(integersIn:dropRow..<(dropRow + draggedStrings.count)))
tableView.reloadData()
return true
}
}
To make dragging to the trash work, subclass NSTableView and override:
override func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor
context: NSDraggingContext) -> NSDragOperation {
let test = super.draggingSession(session, sourceOperationMaskFor: context)
Swift.print("sourceOperationMaskFor \(test)")
switch context {
case .withinApplication:
return [.move, .copy]
case .outsideApplication:
return [.delete]
}
}
p.s. I'm not familiar with Swift and had some trouble with arrays and indexsets so I used NSMutableArray and NSIndexSet.

How to create a NSTableview with custom viewcells

I tried many different ways to create a NSTableview with custom NSTableCellView but I could not make it work. The question is, how can I do that thing?
Here is the last thing I tried:
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cellIdentifier: String = ""
if tableColumn == tableView.tableColumns[0] {
cellIdentifier = "CellID"
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
cell.identifier = cellIdentifier
// array is an array that contains NSView with layers with different colors
cell.myView = array[row]
return cell
}
}
return nil
}
After adding a label:
And the full code:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
tableview.setDelegate(self)
tableview.setDataSource(self)
let view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200))
view.layer?.backgroundColor = NSColor.blueColor().CGColor
array.append(view)
let view2 = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200))
view2.layer?.backgroundColor = NSColor.greenColor().CGColor
array.append(view2)
array2label.append("bu")
array2label.append("buu")
tableview.reloadData()
// Do any additional setup after loading the view.
}
override func viewWillAppear() {
//tableview.reloadData()
laView.layer?.backgroundColor = NSColor.greenColor().CGColor
}
#IBOutlet weak var laView: NSView!
#IBOutlet weak var tableview: NSTableView!
var array = [NSView]()
var array2label = [String]()// = ["bu","buu"]
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
if (tableView.identifier == "Taula") {
return array.count
//return taulaGrafics.count
} else {
return 0
}
}
#IBOutlet weak var viewDeProva: NSView!
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
print ( "Preparem la TableView" )
var cellIdentifier: String = ""
if tableColumn == tableView.tableColumns[0] {
cellIdentifier = "CellID"
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
print ( "aqui" )
print(array)
print(array2label)
cell.identifier = cellIdentifier
cell.myView = array[row]
cell.label.stringValue = array2label[row]
return cell
}
}
return nil
}
#IBAction func afegir(sender: NSButton) {
let view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200))
view.layer?.backgroundColor = NSColor.yellowColor().CGColor
array.append(view)
array2label.append("buLabel")
tableview.reloadData()
}
#IBAction func treure(sender: NSButton) {
array.removeLast()
tableview.reloadData()
}
}
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
print ( "aqui" )
print(array)
print(array2label)
cell.identifier = cellIdentifier
cell.myView = array[row]
cell.myView.wantsLayer = true
cell.label.stringValue = array2label[row]
return cell
}
With this changes the view show the color that is expected to be:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
print("DidLoad")
tableview.setDelegate(self)
tableview.setDataSource(self)
}
override func viewWillAppear() {
print("WillAppear")
array.append(NSColor.blueColor().CGColor)
array2label.append("buIni")
tableview.reloadData()
laView.layer?.backgroundColor = NSColor.greenColor().CGColor
}
#IBOutlet weak var laView: NSView!
#IBOutlet weak var tableview: NSTableView!
var array = [CGColor]()
var array2label = [String]()
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
if (tableView.identifier == "Taula") {
return array.count
//return taulaGrafics.count
} else {
return 0
}
}
#IBOutlet weak var viewDeProva: NSView!
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
print ( "Preparem la TableView" )
var cellIdentifier: String = ""
if tableColumn == tableView.tableColumns[0] {
cellIdentifier = "CellID"
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
print ( "Here" )
print(array)
print(array2label)
cell.identifier = cellIdentifier
cell.myView.layer?.backgroundColor = array[row]
cell.label.stringValue = array2label[row]
return cell
}
}
return nil
}
#IBAction func afegir(sender: NSButton) {
let color = NSColor.yellowColor().CGColor
array.append(color)
array2label.append("buLabel")
tableview.reloadData()
}
#IBAction func treure(sender: NSButton) {
array.removeLast()
array2label.removeLast()
tableview.reloadData()
}
}
The problem was that the NSView inside the NSTableViewCell can't be replaced by another view, because what you do doing that is changing the prototype cell. So if you replace the view the NSTableViewCell doesn't recognize that NewView layer (I'm not sure 100% why). Looks like just some information is shared.
So, how can we pass info to the tableview cell? How can we show a layer there?? Well the answer is that just modifying the prototype cell NSTableCellView added on the interface builder or its subclass (like my case, see bellow the cell). Modifying its content, but not the NSView!, inside the viewForTableColumn function. See in this piece of code:
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
cell.identifier = cellIdentifier // that is to identify the cell
cell.myView.layer?.backgroundColor = array[row] // see that the array contains CGCOLORS not NSViews
// The program was not showing the colors because the
// view copied doesn't copy its layer, or at least
// doesn't show it.
// Even though, if you say: *******---This is incorrect:
cell.myView = arrayOfNSViews[row] //the program will
// crash when removing 2 rows, myView = nil !!!!.
// That is because when you remove the item of the array
// somehow you are removing myView too, because it make a
// copy or a reference to it (Not sure what exactly).
// Here continues the correct program: ******---
cell.label.stringValue = array2label[row]
return cell
}
Also see that in the textField case: cell.label.stringValue = array2label[row], you change the string value of the textfield, not the whole NSTextfield.
So guys remember and repeat my words: "I'm not going to change the view of the cell, just its properties". I just spend 4 days to find that...
Here is the NStableCellView promised:
class MyTableCellView: NSTableCellView {
#IBOutlet weak var myView: NSView!
#IBOutlet weak var label: NSTextField!
}
One Image of the view hierarchy:

Populating NSTableView from array in Swift (without XCode)

I'm trying to develop for OS X without the use of XCode in Swift. I'm running into extreme headaches trying to populate NSTableView from any kind of data source. Here is my code:
import Cocoa
class StupidDataSource: NSObject, NSTableViewDataSource, NSTableViewDelegate {
var lib:[NSDictionary] = [["a": "1", "b": "2"],
["a": "3", "b": "4"]]
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return lib.count
}
func tableView(tableView: NSTableView,
objectValueForTableColumn tableColumn: NSTableColumn?,
row: Int) -> AnyObject? {
let result = lib[row].objectForKey(tableColumn!.identifier)
return result
}
}
func make_table(window: NSWindow,
_ size:(x:Int, y:Int, width:Int, ht:Int),
_ title:String,
_ data:StupidDataSource
)-> NSTableView {
let tableContainer = NSScrollView(frame:NSMakeRect(0, 0, 400, 400))
let tableView = NSTableView(frame: NSMakeRect(0, 0, 400, 400))
tableView.setDataSource(data)
tableView.reloadData()
tableContainer.documentView = tableView
tableContainer.hasVerticalScroller = true
window.contentView!.addSubview(tableContainer)
return tableView
}
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
func applicationDidFinishLaunching(aNotification: NSNotification)
{
set_window_args(window, 400, 400, "Ass")
let dumb_dict = StupidDataSource()
let tableytable = make_table(window, (100, 100, 0 ,0), "poop", dumb_dict)
window.makeKeyAndOrderFront(window)
window.level = 1
}
}
let app = NSApplication.sharedApplication()
app.setActivationPolicy(.Regular)
let controller = AppDelegate()
app.delegate = controller
app.run()
This can be run at the command line with "swift file.swift". I have implemented the methods required for NSTableViewDataSource, and initialized everything correctly according to Apple's documentation, yet nothing shows up in the table. What is missing?
I'm no expert but it looks to me as though your tableView function is just returning the contents of the relevant row from the dictionary.
I personally return a cell as an NSView? like:-
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
if let cell = myViewBasedTableView.makeViewWithIdentifier(tableColumn!.identifier, owner: nil) as? NSTableCellView
{
cell.textField!.stringValue = "Hello";
// Instead of "Hello". Put the contents of one element from your
// dict in here. This func is called automatically for every cell
// in the grid.
return cell;
}
return myEmptyDefaultCell;
}
Having said that maybe your example is correct for Cell Based tableView. I never used one of them so don't know. I always use viewBased table view. I often put little images/icons in the cells as well as text using code like:
cell.textField!.stringValue = "Hello";
cell.imageView!.image = smileyFace;
return cell;

How to display label when table view is empty

I am trying to figure out how to display a message within a table view when the table is empty. I would like it to say something like: "You haven't added any transactions yet. Tap the add button to get started.". Obviously I would need it to revert back to this message if the user deletes all of the cells, too.
This is the code that I currently have in my table view controller:
class ThirdViewController: UITableViewController {
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return arrayObject.paymentsArray().count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:CustomTransactionTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
cell.paymentNameLabel.text = (arrayObject.paymentsArray().objectAtIndex(indexPath.row)) as String
cell.costLabel.text = (arrayObject.costArray().objectAtIndex(indexPath.row)) as String
cell.dateLabel.text = (arrayObject.dateArray().objectAtIndex(indexPath.row)) as String
if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
return cell
}
override func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
if let tv=tableView {
arrayDataCost.removeObjectAtIndex(indexPath!.row)
arrayDataImage.removeObjectAtIndex(indexPath!.row)
arrayDataPayments.removeObjectAtIndex(indexPath!.row)
arrayDataDate.removeObjectAtIndex(indexPath!.row)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
}
}
Any help would be much appreciated!
You might want to set the backgroundView to a UILabel (Or some view you made when the table is empty
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.numberOfRow == 0{
var emptyLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height))
emptyLabel.text = "No Data"
emptyLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = emptyLabel
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
return 0
} else {
return self.numberOfRow
}
}
something like this works fine for me
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
var numOfSection: NSInteger = 0
if array.count > 0
{
self.tableView.backgroundView = nil
numOfSection = 1
}
else
{
var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
noDataLabel.text = "No Data Available"
noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
noDataLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = noDataLabel
}
return numOfSection
}
Override your viewDidLoad() method like this:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.yourLabel);
if arrayObject.paymentsArray().count > 0 {
self.tableView.hidden = NO;
self.yourLabel.hidden = YES;
} else {
self.tableView.hidden = YES;
self.yourLabel.hidden = NO;
}
}
You can either hide the tableView and unhide the label, or display some sort of animation that reduces the alpha of one of the views for a 'fade' effect.
You can use this method. No function limit
Swift3
if tableView.visibleCells.isEmpty{
//tableview is not data
print("can not found data")
}else{
//do somethings
}
I needed to achieve the same thing. This is what i did.
var label: UILabel {
let label = UILabel(frame: tableView.bounds)
label.text = "empty"
return label
}
override func viewDidLoad() {
tableView.backgroundView = UIView(frame: tableView.bounds)
tableView.backgroundView?.addSubview(noPlacesMessageLabel)
}

Resources