我正在构建一个小应用程序来学习 Objective-C。我已经尝试阅读一些书籍,认为只是“跳入”将是我们学习的最佳方式。
不过现在我有点卡住了,我看过苹果的一些例子(我相信我或多或少地在关注它们)。无论如何,我的问题是这样的:
当我创建一个包含表格的窗口时,给窗口一些数据,然后打开窗口,我有数据但没有 tableView(可能是因为在我打开窗口之前没有加载 NIB?)。当我打开窗口时,我的 numberOfRowsInTableView 函数似乎无法找到我的数据。
我已经包含了 .h 和 .m 文件的完整代码,希望你能看到问题所在..(可能真的很愚蠢)
//
// MainWindow.h
// Practice App
//
//
#import <Cocoa/Cocoa.h>
#import "mvmUser.h"
@interface FriendStreamController : NSWindowController<NSTableViewDelegate, NSTableViewDataSource>
@property (assign) IBOutlet NSTableView *myTableView;
@property (nonatomic, retain) mvmUser *user;
@property (nonatomic, retain) NSArray *activityData;
-(void)setUserObject:(mvmUser *)user;
-(void)createFriendstream;
@end
和
//
// MainWindow.m
// Practice App
//
//
#import "FriendStreamController.h"
@interface FriendStreamController ()
@end
@implementation FriendStreamController
@synthesize myTableView = _myTableView;
@synthesize user = _user;
@synthesize activityData = _activityData;
-(NSString *)windowNibName {
return @"FriendStreamController";
}
- (id)initWithWindow:(NSWindow *)window
{
return self;
NSWindow *selfWindow = self.window;
[selfWindow makeKeyAndOrderFront:self];
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
-(void)createFriendstream{
[self.myTableView reloadData];
}
-(void) setUserObject:(mvmUser *)user {
self.user = user;
_activityData = [[NSArray alloc]initWithArray:self.user.apiResponse copyItems:YES];
NSLog(@"data 1st: %@, %@", _activityData, _myTableView);
}
-(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView {
NSLog(@"data 2nd: %@, %@", _activityData, _myTableView);
int numRows = [_activityData count];
return [_activityData count];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSLog(@"table view stuff");
// group the model (activityData)
NSDictionary *dictionary = [_activityData objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
if([identifier isEqualToString:@"ActivityCell"]) {
NSTableCellView *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
cellView.textField = [dictionary objectForKey:@"username"];
return cellView;
}
return nil;
}
@end
我的 NSLOG 给了我:
2012-07-13 00:07:14.550 Practice App[13571:f03] data 1st: (
{
"activity_id" = 3122267;
"activity_name" = "add_media";
avatar = "http://a1.media.mobyhub.com/avatar/41d3b6664cb8d8a55148364f4833c89e_60px.jpg";
"created_on" = "2012-07-12 23:37:50";
id = 3122267;
moby = {
"content_type" = photo;
description = "";
id = 13271224;
media = "http://a1.img.mobypicture.com/69f025622e87b1265d55395d877f1d83_view.jpg";
"short_url" = "http://moby.to/f8asii";
thumbnail = "http://a1.img.mobypicture.com/69f025622e87b1265d55395d877f1d83_square.jpg";
title = "@robertpaul @Paulpridephoto @mattebox @eastbayjay Set this to on. Preset URL saved in image EXIF data.";
url = "http://www.mobypicture.com/user/simon_harper/view/13271224";
};
"name_first" = Simon;
"name_last" = Harper;
"user_id" = 454519;
username = "simon_harper";
},
{
"activity_id" = 3119852;
"activity_name" = "add_media";
avatar = "http://a0.media.mobyhub.com/avatar/c2b0b2914f2738a7ded180fee123ad91_60px.jpg";
"created_on" = "2012-07-12 20:07:59";
id = 3119852;
moby = {
"content_type" = photo;
description = "";
id = 13269679;
media = "http://a0.img.mobypicture.com/d86fc26985b5cbf34b3a0aa3d3b40d5c_view.jpg";
"short_url" = "http://moby.to/tphlja";
thumbnail = "http://a0.img.mobypicture.com/d86fc26985b5cbf34b3a0aa3d3b40d5c_square.jpg";
title = "Iemand een idee hoe ik met dit bedrijf in contact kan komen? Formulier werkt niet, maar kan geen vragen stellen @nrc ";
url = "http://www.mobypicture.com/user/Josk/view/13269679";
};
"name_first" = Jos;
"name_last" = Kok;
"user_id" = 2540;
username = Josk;
},
{
"activity_id" = 3118440;
"activity_name" = "add_media";
avatar = "http://a0.media.mobyhub.com/avatar/7e4bcc3f625b3d50ed49bd32eb65eafc_60px.jpg";
"created_on" = "2012-07-12 17:55:26";
id = 3118440;
moby = {
"content_type" = photo;
description = "";
id = 13268834;
media = "http://a3.img.mobypicture.com/ebfc0300aeb3bd54f1956cb22eea8cbb_view.jpg";
"short_url" = "http://moby.to/sg62tw";
thumbnail = "http://a3.img.mobypicture.com/ebfc0300aeb3bd54f1956cb22eea8cbb_square.jpg";
title = "Opel Ampera op de A9 vlakbij haarlem. Best mooie auto eigenlijk. Hoeveel rijd hij nu echt elektrisch op een volle accu?";
url = "http://www.mobypicture.com/user/vincente/view/13268834";
};
"name_first" = vincent;
"name_last" = everts;
"user_id" = 2487;
username = vincente;
},
{
"activity_id" = 3118321;
"activity_name" = "add_media";
avatar = "http://a0.media.mobyhub.com/avatar/402bff232d440ecf6a4a037830eb2780_60px.jpg";
"created_on" = "2012-07-12 17:39:40";
id = 3118321;
moby = {
"content_type" = photo;
description = "";
id = 13268749;
latlong = "52.067,4.32418";
media = "http://a2.img.mobypicture.com/d27abd1ac8b18f406f45f8621ef0e2d7_view.jpg";
"short_url" = "http://moby.to/3j1wie";
thumbnail = "http://a2.img.mobypicture.com/d27abd1ac8b18f406f45f8621ef0e2d7_square.jpg";
title = "Trots op @ChelseyWinkel die nu al haar P haalt :)";
url = "http://www.mobypicture.com/user/Mikevanrossum/view/13268749";
};
"name_first" = Mike;
"name_last" = "van Rossum";
"user_id" = 28504056;
username = Mikevanrossum;
},
{
"activity_id" = 3117532;
"activity_name" = "add_friend";
avatar = "http://a1.media.mobyhub.com/avatar/f1459fef5a78c525ef60c47a706709bd_60px.jpg";
"created_on" = "2012-07-12 16:33:49";
id = 3117532;
"name_first" = Berry;
"name_last" = Helsloot;
target = {
avatar = "http://a0.media.mobyhub.com/avatar/e0f6b16e645594c96a6d8693969c787d_60px.jpg";
"name_first" = Mick;
"name_last" = Harren;
"user_id" = 427052;
username = mickharren;
};
"user_id" = 213700;
username = bhelsloot;
},
{
"activity_id" = 3116839;
"activity_name" = "add_media";
avatar = "http://a0.media.mobyhub.com/avatar/601a85e2a0279875ea558835fda56aef_60px.jpg";
"created_on" = "2012-07-12 15:23:29";
id = 3116839;
moby = {
"content_type" = photo;
description = "";
id = 13268001;
media = "http://a3.img.mobypicture.com/7bf755f874955c24bfa06e247d0d11ea_view.jpg";
"short_url" = "http://moby.to/yqg56x";
thumbnail = "http://a3.img.mobypicture.com/7bf755f874955c24bfa06e247d0d11ea_square.jpg";
title = "\U266c'Ice Ice Baby' - Vanilla Ice\U266a #nowplaying on #spotify http://open.spotify.com/track/3XVozq1aeqsJwpXrEZrDJ9";
url = "http://www.mobypicture.com/user/Dryxe/view/13268001";
};
"name_first" = Daan;
"name_last" = "van der Plas";
"user_id" = 28780688;
username = Dryxe;
}
), (null)
2012-07-13 00:07:14.655 Practice App[13571:f03] data 2nd: (null), <NSTableView: 0x7f887b364930>
希望你能提供一些帮助,会很棒。