开发者

Why is my table view not displaying my data?

I have a document based application which has two classes, myDocument and Person. The interface consists of a table view for displaying the data and two buttons for adding and removing a peson from the table. The myDocument class is the data source and the delegate. I have the necessary delegate methods implemented but when I click the button to add a record to the table the table view does not show the change. Can someone show me what I'm doing wrong?

#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 =开发者_StackOverflow中文版 [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


Check the _tableView outlet is actually being initialised to point to the table view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜