Pre-selecting row in NSTableView not working - macos

I am using the code below in viewDidLoad() to try and pre-select a table row in a tableView object (tblProjectNumber). No errors at compile or run time.
Everything loads ok and I can select table rows by a mouse click without any problems once the view is loaded. However, I cannot load the view with the required default row pre-selected. I have tried the following code in viewDidLoad() (the last three lines refer). The tableView does not show a selected row and does not scroll the row at index(100) when the data loads.
override func viewDidLoad() {
super.viewDidLoad()
self.view.wantsLayer = true
self.view.layer?.backgroundColor = NSColor.windowBackgroundColor().CGColor
self.view.window?.title = "Project Detail Module"
self.view.window?.titleVisibility
// sort the list of project numbers so that they display correctly when the view is first loaded
self.sortProjectNumberArray()
self.loadProjectRowDataArray()
self.sortPPRFilesForLatestDate()
// pre-select table row to load on initial display of the tableView
let index = NSIndexSet(index: 100)
self.tblProjectNumber.scrollRowToVisible(100)
self.tblProjectNumber.selectRowIndexes(index, byExtendingSelection: true)
}
The answeres here:
Select row in tableView programmatically by other method beside calling their indexPath
Select tableview row programmatically
Xcode - Swift Programmatically select tablecell
have helped but I haven't been able to solve the issue. Also the call to selectRowAtIndexPath used in the last link doesn't seem to be available in Swift so it doesn't work for me either.

Try your code inside tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? method.
Don't know which select method will work in OSX but selectRowAtIndexPath is the correct method for iOS. So try every possible selectIndex method of OSX but inside the viewForTableColumn method through which the tabledata is actully being displayed. i.e. while displaying tabledata..it will check for the index you want to be selected.
Hope this may help !

Related

Cannot get NSTableView to populate

In a Swift OSX Cocoa program, I cannot for the life of me get any text to display from a simple two-dimensional array into a two-column NSTableView. The Table View is inside a window in the xib. The dataSource outlet and delegate outlet, and tableView referencing outlet, are all connected to File's Owner. In the controller's .swift code file, I have declared
#IBOutlet weak var tableView: NSTableView!
and that is connected to the xib, so my problem doesn't seem to be the same as [this one]. I want to populate tableView with an array, but like this person I simplified it to populating with a string literal just to try and get any text at all. (The previous poster never seems to have gotten an answer, but he didn't post a code snippet so I will do that.) numberOfRowsInTableView returns 2, as it's currently supposed to. And here's tableView, excluding currently commented-out code:
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!
{
var newString = ""
newString = "Help!"
return newString
}
As far as I can tell, I should get a 2x2 table with each cell containing the word "Help!" But I don't. The table remains empty. Why?
EDIT in response to comment below: The commenter recommends I drag to Application under Objects, which I think is referring to something like this?
For whatever reason, I don't have that. My document outline for the .xib looks like this:
Maybe that's a clue as to what's wrong?

Set NSTextField frame height in tableView viewForTableColumn not working

I have a .xib with 2-3 NSTextFields displayed in it (as shown below). I would like to resize one of these NSTextFields so it best fits the content it is displaying. My problem is that I cannot change the size of the Text Field in the viewForTableColumn at all.
I have successfully set my heightOfRow. That is working nicely.
I have turned off AutoLayout for the field as it wasn't quite working properly for me.
Now in tableView viewForTableColumn I have the following code:
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier("MessageView", owner: self) as! MessageView!
var newFrame = cell.messageSubject.frame
newFrame.size.height = 200
cell.messageSubject.frame = newFrame
return cell
}
As you can see the frame for the Subject field remains unaltered.
Interestingly the size of the frame is always the default size set in the Interface builder even when AutoLayout is turned on.
My conclusion is that I'm trying to do this in the wrong method.
I have been able to resize the frame in a simple non-table based viewController with no problem.

Xcode custom UITableViewCell has unwanted image indentation after selection

Using Xcode 6.3.1 and iOS 8.3 (and Swift 1.2 for what it matters here) I created a custom UITableViewCell that includes an UIImageView, and two UILabels. The whole thing has been formatted using Auto-Layout. No errors, no warnings. When I run the app, all goes fine until... I selected a table row. In that case the image indents a little (simply selection, no editing enabled) and remains indented.
See the first row of the image to get an impression:
Hiding the image removes the problem, and setting the UITableViewCell's Selection parameter (in Xcode) to None also removes the problem (but that feels unnatural when selecting a row).
I am not sure whether it is Auto Layout related as it only appears after selecting the row, but I do not know how to fix it properly. Any ideas?
Edit: here is my cell code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(customCellIdentifier, forIndexPath: indexPath) as! MenuTableViewCell
let item = menuItems[indexPath.row]
cell.titleLabel!.text = item.title
cell.subtitleLabel?.text = item.subTitle
cell.imageView?.image = UIImage(named: item.icon)
return cell
}

Why does makeViewWithIdentifier:owner: return different types sporadically?

Hello and thanks for reading. I am trying to populate a view based NSTableView from an array of People objects. In my setup i'm using Storyboards with two xib files (one for the main tableview and the other for my custom view).
The call to makeViewWithIdentifier:owner: within the delegate method "tableView viewForTableColumn row" is returning different types for no obvious reason. Sometimes when i compile it returns objects of type "MyOView" (see Console Output 1) which is my custom view class and yet other times when i compile (despite literally no change in my code) it returns NSTextField (see Console Output 2)
Why is this happening?
Console Output 1:
class name of cell is: Saddle.MyOView
class name of cell is: Saddle.MyOView
Console Output 2:
Could not cast value of type 'NSTextField' (0x7fff7e1bbf40) to 'Saddle.MyOView' (0x100017980).
(lldb)
Here is my implementation of tableView viewForTableColumn Row...
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cell = tableView.makeViewWithIdentifier("MyOView", owner: self) as! MyOView
println("class name of cell is: " + cell.className)
let person : Person = allPeopleInRace[row]
cell.itemNumber.stringValue = person.number.description
cell.itemName.stringValue = person.name
return cell
}
In the ViewController in viewDidLoad() i have registered the second nib
let nib = NSNib(nibNamed: "MyOView", bundle: NSBundle.mainBundle())
mainTableView.registerNib(nib!, forIdentifier: "MyOView")
Any help would be appreciated :)
thanks
After some more digging around I realised that I hadn't named my custom cell Xib. Hope this is helpful to someone else.

Return to specific UITableView Cell

I have 2 views.
On the second I have a UITableview where I can choose the content to view in the first window.
When I return to the second view containing the UITableView, is there an easy way to return to the cell representing the content on the main page, rather than scrolling down to it?
I'm assuming if I know the record number of the array that created the UITableview, which is the same array that I'm populating the main page with, then I should just be able to jump to that index number on the UITableView. I can't figure out how to get the index number of the current record.
Sorry this is such a convoluted question.
if I understood you question correctly, to update the currently selected row in the previous tableView, you can invoke a method via delegate and perform something like
-(void) updateSelectedRow:(NSInteger)row {
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}

Resources