我一直在嘗試通過大量搜索和觀看視頻來解決過去幾個小時這個問題。這是我想要做的:NSOutlineView將不會重新加載keydown上的數據,但重點更新
摘要:一個簡單的窗口允許我將郵件從我的收件箱移動到我的許多郵件文件夾中的任意一個。當我輸入我正在查找的郵件文件夾的名稱時,大綱視圖應自動刷新,只顯示帶有匹配文本的郵件文件夾 - 如果知道它,請考慮MsgFiler。
- 在文本框中
- 當我輸入每個字母鍵入文字,我想NSOutlineView更新它的數據有符合我輸入的文字越來越少的文件夾。 (此刻,我的測試只是增加了一個 「測試郵箱」 進入NSOutlineView
當前結果:
- 在文本框中鍵入文本
- 什麼也沒有發生
- 點擊NSOutlineView ,視圖得到更新
TextfieldDelegate.m:
#import "TextFieldDelegate.h"
#import "OutlineViewController.h"
#import "GetMailDatasource.h"
@implementation TextFieldDelegate
@synthesize testLabel;
- (void) controlTextDidChange :(NSNotification *) sender {
NSTextField *changedField = [sender object];
NSLog(@"in control text did change");
//Just some text code to see the change when text does change
NSString *text = [changedField stringValue];
[testLabel setStringValue:text];
NSLog(@"changed the label and creating the data now");
OutlineViewController *vc = [[OutlineViewController alloc] init];
[vc refreshTheData:sender];
[vc.outlineView reloadData];
[vc release];
}
- (void)controlTextDidEndEditing:(NSNotification *)obj {
NSLog(@"in end editting");
return;
}
@end
OutlineViewController.m
#import "OutlineViewController.h"
#import "GetMailDatasource.h"
@implementation OutlineViewController
- (id) init {
self = [super init];
if (self) {
_mailboxes = [[NSMutableArray alloc] init];
if (myMailboxes != nil) {
_mailboxes = myMailboxes;
} else {
GetMailDatasource *mailDatasource = [[GetMailDatasource alloc] init];
[mailDatasource createFakeData];
_mailboxes = myMailboxes;
}
}
NSLog(@"inited outline view controller");
return self;
}
- (IBAction) refreshTheData : (id) sender {
NSLog(@"in refreshTheData");
Mailbox *m = [self.outlineView itemAtRow:[self.outlineView selectedRow]];
if (m)
[m addChild:[[Mailbox alloc] init]];
else
[self.mailboxes addObject:[[Mailbox alloc] init]];
NSLog(@"running reloadData");
[self.outlineView reloadData];
}
#I know these work, but providing them for completeness
#pragma mark NSOutlineView Data Source Methods
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return !item ? [self.mailboxes count] : [[ item children] count ];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return !item ? YES : [[item children ] count] != 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return !item ? [self.mailboxes objectAtIndex:index] : [[item children] objectAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [item name];
}
@end
我有,我在界面上添加一個單獨的「測試按鈕」只是嘗試,我已經束縛它的「已發送行動」直接到「refreshTheData 「在OutlineViewController上的功能,並且工作順利。在NSOutlineView中添加新項目並立即更新。
我不知道還有什麼是必要的,試圖找出問題所在......任何建議將不勝感激!
非常感謝!
我該如何在屏幕上使用大綱視圖?我已經嘗試過不同的方式來做到這一點,但是當我在TextFieldDelegate.h中爲NSOutlineView創建屬性並嘗試執行類似[outlineView refreshTheData]的操作時,我收到消息「outlineView可能無法響應refreshTheData」,任何想法? –
只需添加,在我的TextFieldDelegate.h中,我引用屬性如下: –
@property(retain)IBOutlet NSOutlineView * outlineView; (儘快輸入) –