2012-11-11 112 views
-1

我有搜索网站,了解如何创建自定义表格视图(因为我想隐藏导航栏为特定视图),我正在按照每一步。但是,我的结果不显示表格。自定义TableViewController,不显示

这里是我的.h文件

#import <UIKit/UIKit.h> 

@interface HallFameControllerViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource>{ 

    NSArray *leaders; 
} 

@property (strong, nonatomic) NSArray *leaders; 

@end 

和我的.m文件

#import "HallFameControllerViewController.h" 

@interface HallFameControllerViewController() 

@end 

@implementation HallFameControllerViewController 

@synthesize leaders; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    leaders = [NSArray arrayWithObjects:@"Player #1", @"Player #2", @"Player #3", nil]; 
} 

- (void) viewDidUnload{ 

    self.leaders = nil; 
} 

- (void) viewWillAppear:(BOOL)animated{ 

    [self.navigationController setNavigationBarHidden:NO]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [leaders count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell. 
    cell.textLabel.text = [self.leaders objectAtIndex: [indexPath row]]; 
    return cell; 
} 
@end 

在我的故事板,我创建了一个视图控制器和里面我有1个标签和1周的TableView。我为我的ViewController设置了自定义类为“HallFameControllerViewController”,数据源,表视图的代理也设置为“HallFameControllerViewController”。结果,标签在那里但没有表格。

我有一些printf()语句在侧.m文件,viewDidLoad()执行,但cellForRowAtIndexPath()不!

我在做什么错在这里?另外,cellIdentifier是什么,为什么设置为“Cell”(自动)?

在此先感谢。

+0

已复制,已答复x次。 numberOfSectionsInTableView = 1 –

+0

@ Daij-Djan有更多的问题,而不是错误的部分数量。 – rmaddy

回答

0

您需要延长UITableViewController而不是UIViewController。然后,您不需要将UITableViewDataSourceUITableViewDelegate协议添加到您的接口声明中。

您可以使用普通视图控制器,但它的更多工作。你从来没有实际上在任何地方添加过UITableView。你只需实现表格视图方法(你仍然需要这样做)。

您需要更新您的numberOfSectionsInTableView:。即使修正了其他问题,返回0也会给你一个空表。

所以更改此设置:

@interface HallFameControllerViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{ 

到:

@interface HallFameControllerViewController : UITableViewController { 

并改变这一点:

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

到:

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

谢谢@rmaddy :) – toto7