NSTableViewを複数使用する [AppleScript辞書はつくれるか?]
テーブルビューを2つまたはそれ以上使う場合に、ちょっとしたテクニックが必要だったのでメモ。
numberOfRowsInTableView_ と tableView_objectValueForTableColumn_row_ の両方に、どのテーブルの情報を使いたいのかを判断させなければいけないようだ。
if targetTableView = aTableView1 then ~
というような、Applescriptならできる、オブジェクトを直接比較するちょっと大胆な判断式を使ってみたらうまくいった。
property theDataSource1 : {} global aTableView1, sourceList1 property theDataSource2 : {} global aTableView2, sourceList2
on applicationWillFinishLaunching_(aNotification) (* NSWindow - theWindow用 *) tell theWindow setTitle_("Window") set {window_w,window_h} to {616,210} setContentSize_(current application's NSMakeSize(window_w,window_h)) setMaxSize_(current application's NSMakeSize(window_w,window_h)) setMinSize_(current application's NSMakeSize(window_w,window_h)) setStyleMask_(7) --7:リサイズのみOff setLevel_(1) end tell
-- ●●●●● NSTableView 1 set {table1_x,table1_y,table1_w,table1_h} to {3 ,3,300,200} set sourceList1 to ¬ {{data1:1,data2:"A"},{data1:2,data2:"B"},{data1:3,data2:"C"},{data1:4,data2:"D"},{data1:5,data2:"E"}}
(* データーソースを用意する *) set theDataSource1 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource1's addObjectsFromArray_(sourceList1)
(* コラムデータ(列)を作成 *) 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") end tell
(* コラムデータをテーブルデータにのせる *) set aTableView1 to current application's class "NSTableView"'s alloc()'s ¬ initWithFrame_(current application's NSMakeRect(0, 0, table1_w,table1_h)) tell aTableView1 addTableColumn_(aColumn1) addTableColumn_(aColumn2) setDelegate_(me) setDataSource_(me) setUsesAlternatingRowBackgroundColors_(true) setBackgroundColor_(current application's class "NSColor"'s whiteColor) end tell
(* スクロールビューのコンテンツビューにテーブルデータをのせて配置 *) set aScroll1 to current application's class "NSScrollView"'s alloc()'s ¬ initWithFrame_(current application's NSMakeRect(table1_x,table1_y,table1_w,table1_h)) aScroll1's setDocumentView_(aTableView1) theWindow's contentView()'s addSubview_(aScroll1)
aTableView1's reloadData() --|変更されたテーブルの内容で再表示|
-- ●●●●● NSTableView 2 set {table2_x,table2_y,table2_w,table2_h} to {310,3,300,200} set sourceList2 to {{data3:11,data4:"い"},{data3:12,data4:"ろ"},{data3:13,data4:"は"}, ¬ {data3:14,data4:"に"},{data3:15,data4:"ほ"},{data3:16,data4:"へ"},{data3:17,data4:"と"}}
(* データーソースを用意する *) set theDataSource2 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource2's addObjectsFromArray_(sourceList2)
(* コラムデータ(列)を作成 *) set aColumn3 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data3") tell aColumn3 headerCell()'s setTitle_("タイトル 3") setWidth_(100) end tell set aColumn4 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data4") tell aColumn4 headerCell()'s setTitle_("タイトル 4") end tell
(* コラムデータをテーブルデータにのせる *) set aTableView2 to current application's class "NSTableView"'s alloc()'s ¬ initWithFrame_(current application's NSMakeRect(0, 0, table2_w,table2_h)) tell aTableView2 addTableColumn_(aColumn3) addTableColumn_(aColumn4) setDelegate_(me) setDataSource_(me) setBackgroundColor_(current application's NSColor's ¬ colorWithCalibratedRed_green_blue_alpha_(1.0,0.95,0.98,1.0)) end tell
(* スクロールビューのコンテンツビューにテーブルデータをのせて配置 *) set aScroll2 to current application's class "NSScrollView"'s alloc()'s ¬ initWithFrame_(current application's NSMakeRect(table2_x,table2_y,table2_w,table2_h)) aScroll2's setDocumentView_(aTableView2) theWindow's contentView()'s addSubview_(aScroll2)
aTableView2's reloadData() end applicationWillFinishLaunching_
(* TableViewの表示行数処理(必須) *) on numberOfRowsInTableView_(targetTableView) try if targetTableView = aTableView1 then set c to count of (my theDataSource1) else if targetTableView = aTableView2 then set c to count of (my theDataSource2) end if on error set c to 0 end try return c end numberOfRowsInTableView_
(* TableViewデータの処理(必須) *) on tableView_objectValueForTableColumn_row_(targetTableView, aColumn, aRow) try if targetTableView = aTableView1 then set aRec to (my theDataSource1)'s objectAtIndex_(aRow as number) else if targetTableView = aTableView2 then set aRec to (my theDataSource2)'s objectAtIndex_(aRow as number) end if set aIdentifier to (aColumn's identifier()) as string set aRes to (aRec's valueForKey_(aIdentifier)) on error set aRes to {} end try
return aRes end tableView_objectValueForTableColumn_row_ |
コメント 0