列ヘッダーをダブルクリックして、テーブル ビューの列ヘッダー テキストを変更したいと考えています。そのために、次のような NSTableHeaderCell のサブクラスを作成しました-
@interface NBETableHeaderCell : NSTableHeaderCell<NSTextFieldDelegate,NSTableViewDelegate>
@end
#import "NBETableHeaderCell.h"
@implementation NBETableHeaderCell
- (void)textDidEndEditing:(NSNotification *)notification
{
NSTextView *editor = notification.object;
[self setTitle:editor.string];
[self setHighlighted:NO];
[self endEditing:editor];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if([self isHighlighted])
{
[self drawFocusRingMaskWithFrame:cellFrame inView:controlView.superview];
}
[super drawWithFrame:cellFrame inView:controlView];
}
- (void)drawFocusRingMaskWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[controlView lockFocus];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:cellFrame] fill];
[controlView unlockFocus];
}
@end
テーブルの列ヘッダーのセル クラスを設定します
-(void)setupTableHeader:(id)table {
NSArray *columns = [table tableColumns];
NSEnumerator *cols = [columns objectEnumerator];
NSTableColumn *col = nil;
NBETableHeaderCell *iHeaderCell;
while (col = [cols nextObject]) {
iHeaderCell = [[NBETableHeaderCell alloc] initTextCell:[[col headerCell] stringValue]];
[col setHeaderCell:iHeaderCell];
[[col headerCell] setEditable:YES];
[iHeaderCell release];
}
[table setTarget:self];
[table setDoubleAction:@selector(doubleClickInTableView:)];
}
私が設定したダブルクリックアクションは次のとおりです。
- (void)doubleClickInTableView:(id)sender
{
NSInteger row = [myTableView clickedRow];
NSInteger column = [myTableView clickedColumn];
if(row == -1&& column >= 0)
{
NSTableColumn *tableColumn = [[myTableView tableColumns] objectAtIndex:column];
NSTableHeaderView *headerView = [myTableView headerView];
NBETableHeaderCell *headerCell = [tableColumn headerCell];
id cellEditor = [self.view.window fieldEditor:YES forObject:myTableView];
[headerCell setHighlighted:YES];
[headerCell selectWithFrame:[headerView headerRectOfColumn:column]
inView:headerView
editor:cellEditor
delegate:headerCell
start:0
length:headerCell.stringValue.length];
[cellEditor setDrawsBackground:YES];
}
}
これで、列ヘッダーを編集して新しいテキストを設定できるようになりました。タブアウトすると、新しいテキストが持続します。しかし、新しいテキストを設定し、タブアウトする代わりに他のテーブル列ヘッダーをクリックすると、テキストは変更されず、古い値が保持されます。これは、- (void)textDidEndEditing:(NSNotification *)notification
他のテーブルの列ヘッダーを選択したときにデリゲートが呼び出されないことが原因である可能性があります。このデリゲートは、タブ アウト時にのみ呼び出されます。タブアウトせずにテーブル列のヘッダーセルに新しい値を設定するにはどうすればよいですか?