Here's my code:
test.h
@interface testAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSTableView *test;
NSMutableArray *internalArray;
}
@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) NSTableView *test;
-(id) initWithArray: (NSArray*) objects;
-(int)numberOfRowsInTableView:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
test.m (top pieces missing for simplicity's sake)
@synthesize window;
@synthesize test;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
PSClient *client = [PSClient applicationClient];
NSURL *url = [NSURL URLWithString:@"http://www.apple.com/main/rss/hotnews/hotnews.rss"];
PSFeed *feed = [client addFeedWithURL:url];
internalArray = [NSMutableArray array];
// Retrieve the entries as an unsorted enumerator
NSEnumerator *entries = [feed entryEnumeratorSortedBy: nil];
PSEntry *entry;
// Go through each entry and print out the title, authors, and content
while (entry = [entries nextObject]) {
[internalArray addObject:entry.title];
//NSLog(@"Entry Title:%@", entry.title);
//NSLog(@"Entry Authors:%@", entry.authorsForDisplay);
[test reloadData];
//NSLog(@"Entry Content:%@", entry.content.plainTextString);
}
}
-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
NSLog(@"%@", [internalArray count]);
return [internalArray count];
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSString *string = [internalArray objectAtIndex:rowIndex];
NSLog(@"%@", string);
// when I debug, I get same pointers with invalid data
// each object has "name" message
// this following line gives invalid pointer and
// it crashes
return string;
}
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(开发者_运维技巧int)rowIndex
{
NSString *string = [internalArray objectAtIndex:rowIndex];
NSLog(@"%@", string);
// when I debug, I get same pointers with invalid data
// each object has "name" message
// this following line gives invalid pointer and
// it crashes
return string;
}
@end
In IB I have the tableview (test) hooked up to the app delegate: test, datasource, and delegate
For some reason the table isn't displaying the data, and when I call [test reloadData]; the app crashes
Number one with a bullet - you don't retain internalArray
after you create it. Use one of:
internalArray = [[NSMutableArray] alloc] init];
internalArray = [[NSMutableArray array] retain];
instead of what you currently have:
internalArray = [NSMutableArray array];
Also, I think you probably don't want %@
for the format in this method:
-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
NSLog(@"%@", [internalArray count]);
return [internalArray count];
}
That code will certainly (well, often) cause a crash when you do [tableView reloadData]
.
In applicationDidFinishLaunching:
, you don't need to call reloadData
every time you add an object to internalArray
. Just do it once at the end of that loop.
Your implementation of setObjectValue:forTableColumn:row:
also doesn't make sense. It should look more like this:
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSLog(@"%@", anObject);
[internalArray replaceObjectAtIndex:rowIndex withObject:anObject];
}
Hopefully those are some ideas to get you started.
精彩评论