2012-02-04 80 views
0

我目前在工作中有一个标签栏应用程序,并且遇到了一些障碍。没有数据填充在表视图中,当我尝试加载我的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的进步,这是我需要通过的一个障碍。

+0

把一个NSLog的你inits并检查是否你实际上是进入这些功能。 – whitelionV 2012-02-04 01:18:52

+0

实际上,在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

回答

1

我找到了问题的根源。我在numberOfSectionsInTableView:方法中返回0而不是1。我错误地修改了这个方法,并且这样做是告诉程序没有行部分来填充。

不正确的方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 0; 
} 

正确的方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
1

在你RootViewController.init()方法,你需要的东西是这样的:

self.table.dataSource = self; 
self.table.delegate = self; 

假设table是你UITableView的名称。

+0

我已经添加了代码,但似乎没有解决问题。 – RyzBeyond 2012-02-04 01:03:56

+0

实际上,在createPub方法中实际上有一个NSLog,我可以看到该方法被成功调用了10次。但是,当我尝试读取数组的数目后,我看到一个错误。 (int i = 0; i <10; i ++){[PubStore defaultStore] createPub];}} (@“Step Before%@”,[[[PubStore defaultStore] allPubs] count]); NSLog解冻和错误,并且程序没有完全加载。 错误:线程1程序收到信号exc_bad_access – RyzBeyond 2012-02-04 01:48:08

0

问题出在您的模型中,您需要在某处设置defaultStore ivar。你也不应该从init调用initWithNibName。你不应该混合初始者

0

你的tableviewcell需要一个框架。

在的cellForRowAtIndexPath:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease]; 
cell.frame = CGRectMake(0,0,320,80); // or whatever frame you want