I have search the web to learn how to create custom table view (because I want to hide the navigator bar for a specific view) and I am following each step. However, my result doesn’t display the table.
Here is my .h file
#import <UIKit/UIKit.h>
@interface HallFameControllerViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>{
NSArray *leaders;
}
@property (strong, nonatomic) NSArray *leaders;
@end
and my .m file
#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
In my storyboard, I have created a ViewController and inside I have 1 label and 1 TableView. I have set Custom Class for my ViewController to “HallFameControllerViewController” and dataSource, delegate for Table View to “HallFameControllerViewController” as well. Results, Label is there but no table.
I have some printf() statements in side .m file, viewDidLoad() executed but cellForRowAtIndexPath() no!
What I am doing wrong here? In addition, what is cellIdentifier and why has set to “Cell” (automatically)?
Thanks in advance.
You need to extend
UITableViewController, notUIViewController. Then you don’t need to add theUITableViewDataSourceorUITableViewDelegateprotocols to your interface declaration.You can use a plain view controller but its more work. You never actually added a UITableView anywhere. You simply implemented the table view methods (which you still need to do).
You need to update your
numberOfSectionsInTableView:. Returning 0 is going to leave you an empty table, even after fixed the other issues.So change this:
to:
And change this:
to: