0
我有一個基於文檔的應用程序,它有兩個類,myDocument和Person。該界面由一個用於顯示數據的表格視圖和兩個用於從表格中添加和刪除一個peson的按鈕組成。 myDocument類是數據源和委託。我已經實現了必要的委託方法,但是當我單擊按鈕向表中添加記錄時,表視圖不顯示更改。有人能告訴我我做錯了什麼嗎?爲什麼我的表格視圖不顯示我的數據?
#import "MyDocument.h"
@implementation MyDocument
- (id)init
{
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.
_dataArray = [[NSMutableArray alloc]init];
}
return self;
}
-(IBAction)addPerson:(id)sender
{
Person *newPerson = [[Person alloc] init];
[_dataArray addObject:newPerson];
NSLog(@" how many objects are in the table? %d",[_dataArray count]);
//[newPerson release];
[_tableView reloadData];
}
-(int)numberOfRowsInTableView:(NSTabView*)aTableView
{
return [_dataArray count];
}
- (void)tableView:(NSTableView *)aTableView
willDisplayCell:(id)aCell
forTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
[_dataArray objectAtIndex:rowIndex];
NSLog(@" this is the object? %@",[_dataArray objectAtIndex:rowIndex]);
[_tableView reloadData];
}
- (id) tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
//NSLog(@"this is the object %@",[array objectAtIndex:rowIndex]);
NSLog(@"this is what is in the array %@",[_dataArray objectAtIndex:rowIndex]);
return [_dataArray objectAtIndex:rowIndex];
//return [array replaceObjectAtIndex:[tableView selectedRow ] withObject: @"Micheal jordan"];
}
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
if (outError != NULL) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If the given outError != NULL, ensure that you set *outError when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
if (outError != NULL) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}
@end
我做了插座文件的所有者 – lampShade 2010-09-20 01:43:30
您需要將MyDocument(或文件的所有者)的_tableView出口鏈接到窗口內的實際表格視圖對象。 – grahamparks 2010-09-20 11:34:44