i’m building a table view that has an external delegate controller, and it’s created by code, not by storyboard. I have an issue with cells, they are not show, despite execution is reaching correctly delegate methods:
build method:
-(void)buildCategorias{
CGRect twitterFrame = CGRectMake(105, 83, 600, 568);
categoriasView = [[UIView alloc] initWithFrame:twitterFrame];
CGRect scrollViewFrame = CGRectMake(105, 83, 400, 568);
categoriasTableView = [[UITableView alloc] initWithFrame:scrollViewFrame];
categoriasController = [[categoriasViewController alloc] init];
categoriasController.categorias = [[NSArray alloc] initWithObjects:@"Gafas", @"Relojes", @"Pantalones", @"Deportivas", @"Cazadoras", nil];
[categoriasTableView setDelegate:categoriasController];
[categoriasTableView setDataSource:categoriasController];
[self.categoriasView addSubview:categoriasTableView];
[categoriasTableView reloadData];
[self.view addSubview:categoriasView];
}
Custom cell: categoriasCell.h
@interface categoriasCell : UITableViewCell{
UILabel *title;
}
@property (nonatomic, strong) IBOutlet UILabel *title;
@end
categoriasCell.m
@implementation categoriasCell
@synthesize title;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
Table view delegate:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
categoriasCell *cell = [self.tableView
dequeueReusableCellWithIdentifier:@"categorias"];
if (cell == nil) {
cell = [[categoriasCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"categorias"];
}
cell.title.text = [categorias objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
@end
Table view is empty with no content.
Many thanks for your help
Are there no cells at all, or is there the right number of cells, but they’re empty?
If there are no cells at all, check that you did implement the other required delegate method (
tableView:numberOfRowsInSection:), and that it does in fact returns the number of cells you expect.Otherwise, you are probably missing the code to load the cell from its NIB (I am assuming that, given that you’ve got an IBOutlet in your
categoriasCelland no code to actually add the title label to the view, you intend the cell to come from a NIB). See Apple’s documentation for how to load the cell’s NIB and use it in your delegate method. If you don’t load it from the NIB, the text field will not be there, and so your cells will be empty.And two non-fatal coding/style issues:
UITableViewCellSelectionStyleNoneas the style when initializing the cell. You really should be usingUITableViewCellStyleDefaulthere, since the argument is the cell style, not the selection style.CategoriasCellinstead ofcategoriasCell).