2012-11-11 162 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