コードだけでNSTableViewを作る(1) [AppleScript辞書はつくれるか?]
set aColumn1 to current application's class "NSTableColumn"'s alloc()'s ¬ initWithIdentifier_("data1") tell aColumn1 headerCell()'s setTitle_("タイトル 1") end tell
set aColumn2 to current application's class "NSTableColumn"'s alloc()'s ¬ initWithIdentifier_("data2") tell aColumn2 headerCell()'s setTitle_("タイトル 2") end tell |
set aTableView to current application's class "NSTableView"'s alloc()'s ¬ initWithFrame_(current application's NSMakeRect(0, 0, w, h)) tell aTableView addTableColumn_(aColumn1) addTableColumn_(aColumn2) setDelegate_(me) setDataSource_(me) setBackgroundColor_(current application's class "NSColor"'s whiteColor) end tell |
set aScroll to current application's class "NSScrollView"'s alloc()'s ¬ initWithFrame_(current application's NSMakeRect(x,y,w,h)) aScroll's setDocumentView_(aTableView) |
set mainView to theWindow's contentView()
mainView's addSubview_(aScroll)
|
set sourceList to {{data1:1,data2:"A"},{data1:2,data2:"B"},{data1:3,data2:"C"}} set theDataSource to current application's class "NSMutableArray"'s alloc()'s init()
theDataSource's addObjectsFromArray_(sourceList) |
on numberOfRowsInTableView_(aTableView) set c to count of (my theDataSource) return c end numberOfRowsInTableView_ |
on tableView:aTableView objectValueForTableColumn:aColumn row:aRow set aRec to (my theDataSource)'s objectAtIndex_(aRow as number) set aIdentifier to (aColumn's identifier()) as string set aRes to aRec's valueForKey_(aIdentifier) return aRes end tableView:objectValueForTableColumn:row: |
aTableView's reloadData() |
script AppDelegate property parent : class "NSObject" property theWindow : missing value global mainView property theDataSource : {} global sourceList
on applicationWillFinishLaunching_(aNotification) (* ウインドウの大きさを調整 *) set theSize to current application's NSMakeSize(300,200) tell theWindow setContentSize_(theSize) setMinSize_(theSize) --setMaxSize_(theSize) end tell
(* meinview (content view)の記憶 *) set mainView to theWindow's contentView()
set {x,y,w,h} to {0,0,300,200} set sourceList to {{data1:1,data2:"A"},{data1:2,data2:"B"},{data1:3,data2:"C"}} my makeTableView(x,y,w,h) end applicationWillFinishLaunching_
on makeTableView(x,y,w,h) (* ●●● NSTableView class ●●● *) (* データーソースを用意する *) set theDataSource to current application's class "NSMutableArray"'s alloc()'s init() theDataSource's addObjectsFromArray_(sourceList)
(* コラムデータ(列)を作成 *) set aColumn1 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data1") tell aColumn1 headerCell()'s setTitle_("タイトル 1") setWidth_(100) end tell set aColumn2 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data2") tell aColumn2 headerCell()'s setTitle_("タイトル 2") --setWidth_(200) end tell
(* コラムデータをテーブルデータにのせる *) set aTableView to current application's class "NSTableView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0, 0, w, h)) tell aTableView addTableColumn_(aColumn1) addTableColumn_(aColumn2) setDelegate_(me) setDataSource_(me) setBackgroundColor_(current application's class "NSColor"'s whiteColor) end tell
(* スクロールビューにテーブルデータをのせてコンテンツビューに配置 *) set aScroll to current application's class "NSScrollView"'s alloc()'s initWithFrame_(current application's NSMakeRect(x,y,w,h)) aScroll's setDocumentView_(aTableView) mainView's addSubview_(aScroll)
aTableView's reloadData() --|変更されたテーブルの内容で再表示| end makeTableView
(* TableViewの表示行数処理(必須) *) on numberOfRowsInTableView_(aTableView) set c to count of (my theDataSource) return c end numberOfRowsInTableView_
(* TableViewデータの処理(必須) *) on tableView:aTableView objectValueForTableColumn:aColumn row:aRow set aRec to (my theDataSource)'s objectAtIndex_(aRow as number) set aIdentifier to (aColumn's identifier()) as string set aRes to aRec's valueForKey_(aIdentifier) return aRes end tableView:objectValueForTableColumn:row:
on applicationShouldTerminate_(sender) -- Insert code here to do any housekeeping before your application quits return current application's NSTerminateNow end applicationShouldTerminate_
end script |
コメント 0