NSTableView - anyone tried yet ?

Hi,

I know macoslib has a very basic implementation but I’m wondering if anyone has
tried yet to implement a working TableView? Is this possible at all ? If not where did you end up ?
Where could be the pitfalls ?

It’s not finished yet: https://github.com/UBogun/OSXLib
And wait for the next XDev …

Many months ago I had a working version in my “add ons to macoslib” I don’t know if everything still compiles with the latest Xojo but at the time it worked. Maybe search the forum for it - the file should be on Dropbox still.

I’ve tried my own and I think I works OK. Not yet fully implemnted yet (never will I think) or optimized
but a couple of things work like the listbox.
https://www.dropbox.com/s/3jtz1ah1w232llh/NSTableView1.xojo_binary_project?dl=0

I have added a couple of things. basic sorting, column drag reorder ect, rowcount,columncount. You can basically use it now. :slight_smile:
https://www.dropbox.com/s/3jtz1ah1w232llh/NSTableView1.xojo_binary_project?dl=0

  • focusRing
  • rowHeight
  • intercellSpacing
  • hasHeader
  • headerHeight
  • isRowSelected(row) as boolean
  • event ColumnReorder

[quote=274878:@Ulrich Bogun]It’s not finished yet: https://github.com/UBogun/OSXLib
And wait for the next XDev …[/quote]

Anyone experience with a view based TableView ? I got stuck in the implementation:

tableView:viewForTableColumn:row:

I create a new TextField, set it up and return it. I can see and edit the fields. So good so far.
However, you’ll need to have more implementation to catch the events from the TextField
to set the value you’ve typed in back to your data source. Also, I have no access to a prepared
TextField with idenifier in the nib file so I think you got to set it up manually.

…just out of my head as I don’t have the code on hand:

dim cell as ptr = makeViewWithIdentifier(tableview, columnId, nil)
if cell = nil then
cell = init(allocate(“NSTextField”), NSMakeRect(0,0,30,10))
// set up textfield
end if
return cell

(void)textFieldDidBeginEditing:(NSTextField *)textField
(void)textFieldDidEndEditing:(NSTextField *)textField

latest cell based TableView: https://www.dropbox.com/s/j5ucryp0efjtym2/NSTableView3.xojo_binary_project?dl=0

I have made the NSTableView viewbased which is a bunch of work in contrary to using cells. I’d like to encourage
you all to support / change or make further implementations. There are currently a lot of open questions such as the data model. I hope this can become a community project. Try it out. Currently is has only text views & works fine I think.

https://www.dropbox.com/s/aqfouzx7gwe4djr/NSTableView4_viewbased.xojo_binary_project?dl=0

[quote=275134:@Rob Egal]Anyone experience with a view based TableView ? I got stuck in the implementation:

tableView:viewForTableColumn:row:

I create a new TextField, set it up and return it. I can see and edit the fields. So good so far.
However, you’ll need to have more implementation to catch the events from the TextField
to set the value you’ve typed in back to your data source. Also, I have no access to a prepared
TextField with idenifier in the nib file so I think you got to set it up manually.

…just out of my head as I don’t have the code on hand:

dim cell as ptr = makeViewWithIdentifier(tableview, columnId, nil)
if cell = nil then
cell = init(allocate(“NSTextField”), NSMakeRect(0,0,30,10))
// set up textfield
end if
return cell

(void)textFieldDidBeginEditing:(NSTextField *)textField
(void)textFieldDidEndEditing:(NSTextField *)textField[/quote]

found out myself:

[code]Private Shared Function impl_viewForTableColumn(id as ptr, sel as ptr, tableview as ptr, col as ptr, row as integer) As ptr
declare function makeViewWithIdentifier lib CocoaLib Selector “makeViewWithIdentifier:owner:” (obj as ptr, id as CFStringRef, owner as ptr) as ptr
declare function identifier lib CocoaLib Selector “identifier” (obj as ptr) as CFStringRef

dim view as NSTableView = findObject(id)
if view<>nil then
dim colID as CFStringRef = identifier(col)
dim cell as ptr = makeViewWithIdentifier(tableview,colID,nil)
if cell = nil then
return NSTableView.createView(col,colID,view)
else
return cell
end if
end if

End Function
[/code]

[code]Private Shared Function createView(col as ptr, colID as CFStringRef, view as NSTableView) As ptr
// NSColor
declare function clearColor lib CocoaLib Selector “clearColor” (obj as ptr) as ptr
// NSTextField
declare sub setBordered lib CocoaLib Selector “setBordered:” (obj as ptr, value as Boolean)
declare sub setBackgroundColor lib CocoaLib Selector “setBackgroundColor:” (obj as ptr, col as ptr)
declare sub setDelegate lib CocoaLib selector “setDelegate:” (obj_id as Ptr, anObject as Ptr)
declare sub setAutoresizingMask lib CocoaLib Selector “setAutoresizingMask:” (id as ptr,mask as integer)
declare sub setEditable lib CocoaLib Selector “setEditable:” (id as ptr, value as Boolean)
declare function isEditable lib CocoaLib Selector “isEditable” (id as ptr) as Boolean
declare function cell_ lib CocoaLib Selector “cell” (id as ptr) as ptr
declare sub setLineBreakMode lib CocoaLib Selector “setLineBreakMode:” (id as ptr, mode as integer)
// other
declare sub setIdentifier lib CocoaLib Selector “setIdentifier:” (obj as ptr, id as CFStringRef)
declare function autorelease lib CocoaLib Selector “autorelease” (obj as ptr) as ptr
declare sub setTextField lib CocoaLib Selector “setTextField:” (obj as ptr, value as ptr)

declare sub setStringValue lib CocoaLib selector “setStringValue:” (obj as ptr, value as CFStringRef)

const NSLineBreakByTruncatingTail = 4
dim r as NSRect = NSMakeRect(0,0,30,16)
dim tf as ptr = autorelease(initWithFrame(allocate(“NSTextField”), r))
'setAutoresizingMask(tf, NSViewHeightSizable or NSViewWidthSizable)
setBordered(tf,false)
setBackgroundColor(tf, clearColor(NSClassFromString(“NSColor”)))
setEditable(tf, isEditable(col))
setIdentifier(tf,colID)
setLineBreakMode(cell_(tf),NSLineBreakByTruncatingTail)
dim mycls as ptr = NSClassFromString(kTableCellVewDelegateName)
if mycls=nil then
dim targetClass as new TargetClassHelper(kTableCellVewDelegateName, kSuperClassName)
targetClass.addMethod(“control:textShouldBeginEditing:”, AddressOf impl_textShouldBeginEditing, “B@:@@@”)
targetClass.addMethod(“control:textShouldEndEditing:”, AddressOf impl_textShouldEndEditing, “B@:@@@”)
if not targetClass.setInstanceMethods then
System.DebugLog “creating TextField delegate failed.”
return tf
else
mycls = targetClass.targetID
end if
end if

dim delegateID as ptr =init(alloc(mycls))
delegateMap.value(delegateID) = xojo.Core.WeakRef.Create(view)
setDelegate(tf,delegateID)

'setIdentifier(tf,colID)
return tf
End Function
[/code]

I have set up an NSArrayController which holds the data and is way more efficient in terms of sorting which works fine.
Anyway, is anyone familiar with cocoa bindings ? I need to bind the NSArrayController to the tableview.

https://www.dropbox.com/s/9owtyndwwzajqfe/NSTableView8_viewbased.xojo_binary_project?dl=0

  • reworked the code
  • added NSIndexset
  • addd multiselection support
  • added NSArrayController
  • added NSSortDescriptor
  • added NSTableColumn
  • added a couple of convenice methods for selecting rows
  • fixed TextField backgroundcolor

…ahh well I did a lot of things.

Thanks Rob, I will look at it.

BTW: direct link .

[quote=275842:@Emile Schwarz]Thanks Rob, I will look at it.

BTW: direct link .[/quote]

Yes, good idea,

I must admit, after spending a couple of hours with tableview coding, that the ListBox made by Xojo is a good work.

The thing I loved best is… Column Drag !

Works out of the box owing to the NSArrayController which organizes such things. I’d like to bind
more things between tableview and controller but I haven’t found out yet how to do.

I have added the ability to reorder rows (single/multi) and a method to programmatically edit a cell.
https://www.dropbox.com/s/4nfe5sbzct9rwm5/NSTableView13_viewbased.xojo_binary_project?dl=1

I think this is last public release:
added a settings panel

NSTableView:
-added removeRow with animation
-added insertRow with animation
-added removeColumn
-added hidden flag for NSTableColumn
-added doubleClick
-added contextualClick (for cells; in the header the ConstructContextualMenu will work; see demo)

https://www.dropbox.com/s/uyy8vrxaa7mu99z/NSTableView14_viewbased.xojo_binary_project?dl=1

I am working hard to implement a new data model as well as fixed a couple of things +
made the table more convenience such as autoscroll for a not visible row when editing or tabbing the next row
textfields. I’ve also added a NSTableCellView to adjust the rowHeight + added a NSimageView :slight_smile:
The current TableView has basically a standard NSTableCellView with TextField+ImageView like you get by default in IB.

It would be even more awesome when it is possible to add a container as a cell. :slight_smile:
It then would be highly useable for sure.

This won’t happen as it’s technically not possible but I have a different solution in work which is also
customizable. :slight_smile: