NSTextField Continues to Repeat Header

I created a new OSX application with Swift. Start with a view by dragging it into a table view into a view. Then I pressed Ctrl and dragged from my table view to add AppDelegate as my dataSource and delegate . Finally, ctrl-clicked and dragged from AppDelegate to create an IBOutlet for myTableView to represent the table.

Why is this happening? Using Xcode version 6.1 (6A1027).

As a result, we obtain the following results: Results

 import Cocoa class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var myTableView: NSTableView! func applicationDidFinishLaunching(aNotification: NSNotification?) { } func numberOfRowsInTableView(aTableView: NSTableView!) -> Int { let numberOfRows:Int = getDataArray().count return numberOfRows } func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row rowIndex: Int) -> AnyObject! { var newString: (AnyObject?) = getDataArray().objectAtIndex(rowIndex).objectForKey(tableColumn.identifier) println(newString!) return newString! } func applicationWillTerminate(aNotification: NSNotification?) { // Insert code here to tear down your application } func getDataArray () -> NSArray{ var dataArray:[NSDictionary] = [["FirstName": "Debasis", "LastName": "Das"], ["FirstName": "Nishant", "LastName": "Singh"], ["FirstName": "John", "LastName": "Doe"], ["FirstName": "Jane", "LastName": "Doe"], ["FirstName": "Mary", "LastName": "Jane"]]; //println(dataArray); return dataArray; } } 
+5
source share
2 answers

A Table View cell was also added when I dragged it into a Table View along with a text cell. I really don't know why, but this answer from 2 years ago helped me figure this out. Display only NSTableView "Table View Cell"

+2
source

I noticed that "objectValueForTableColumn" only works if there is no other cell to it. Since Xcode6.1 automatically creates one, this method will no longer work unless you delete a new cell.

To save these cells and even add more, we should use "viewForTableColumn" instead: in IB, I called my "table box" custom, and then created an NSTableCellView to target it.

I also added another cell to IB, ImageView, to illustrate my example.

 let myQuotes: [String] = ["text field inside cell one", "number two", "here the tird", "four: a fourth one", "five: and a last one"] func numberOfRowsInTableView(tableView: NSTableView!) -> Int { return myQuotes.count } func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView! { var result: NSTableCellView = tableView.makeViewWithIdentifier("custom", owner: self) as NSTableCellView result.textField.stringValue = myQuotes[row] let myAvatar: NSImage = NSImage(byReferencingFile: "Pictures/avatar.png") result.imageView.image = myAvatar return result } 

I got this working after I crossed this question and this Objective-C example for some time.

+1
source

Source: https://habr.com/ru/post/1202603/


All Articles