我目前在工作中有一个标签栏应用程序,并且遇到了一些障碍。没有数据填充在表视图中,当我尝试加载我的TableViewController从一个NSMutable数组的记录。UITableViewController不能使用NSMutableArray填充tableview
RootViewController的方法 - 这是TableViewController
- (id)init
{
//Call the superclass's designated initalizer
self = [super initWithNibName:nil bundle:nil];
if (self){
//Populate NSMutalableArry with all pub objects
for(int i=0; i<10; i++){
[[PubStore defaultStore] createPub];
//j = [[[[PubStore defaultStore] allPubs] objectAtIndex:i] getPubName];
}
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a label
[tbi setTitle:@"Pubs"];
//Create a UIImage from a file
UIImage *i = [UIImage imageNamed:@"88-beermug.png"];
//Put Image on the tab bar item
[tbi setImage:i];
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[PubStore defaultStore] allPubs] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create an instnce of UITableViewCell, with default appearance
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
//Set the text on the cell with the description of the possession
//that is at the nth index of the possessions, where n =row this cell
//will appear in on the tableview
Pub *p = [[[PubStore defaultStore] allPubs] objectAtIndex:[indexPath row]];
NSLog(@"Display Pub Name: %@", [p getPubName]);
[[cell textLabel] setText: [p getPubName]];
return cell;
}
PubStore方法
- (id)init
{
if (defaultStore) {
//Return the old Default Store
return defaultStore;
}
self = [super init];
if(self){
allPubs = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)allPubs
{
return allPubs;
}
- (Pub *)createPub
{
//* random possessions does not exist in my Pub Class
Pub *p = [Pub randomPub];
NSLog(@"New Pub: %@", p);
[allPubs addObject:p];
return p;
}
任何帮助,您可以提供我将不胜感激,我在学习目标C的进步,这是我需要通过的一个障碍。
把一个NSLog的你inits并检查是否你实际上是进入这些功能。 – whitelionV 2012-02-04 01:18:52
实际上,在createPub方法中实际上有一个NSLog,我可以看到该方法成功调用了10次。但是,当我尝试读取数组的数目后,我看到一个错误。 for(int i = 0; i <10; i ++){[PubStore defaultStore] createPub]; (@“Step Before%@”,[[[PubStore defaultStore] allPubs] count]); – RyzBeyond 2012-02-04 01:41:17