John Cashew
John Cashew

Reputation: 1084

How do I get this UITableView to display?

I can't figure out how to get this UITableView to display.

The code creates the table fully programmatically except for the nib.

The nib is a simple cell that works in other UITableViews in the same app.

When the table view is created and added as a subview to the view and reloadData is called then numberOfSectionsInTableView: and tableView:numberOfRowsInSection: are both called. They both return 1. (I checked in the debugger.)

However tableView:cellForRowAtIndexPath: is never called.

I add the tableView to it's parent view with a NSLayoutConstraint that forces it's height, but nothing is displayed in that spot.

The view hierarchy debugger shows nothing in that spot and that there isn't a view on top of the table.

The app runs without any error or crashes.

I would appreciate your help.

@interface MyViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
@end

@implementation MyViewCell
@end

@interface MyDataTableEntry : NSObject
@property (nonatomic, strong) NSString* title;
@end

@implementation MyDataTableEntry
@end

@interface MyDataTable : NSObject <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *rows;
@property (strong, nonatomic) UINib* nib;
@end

@implementation MyDataTable

- (UINib*)getAndRegisterNib:(NSString*)reuseIdentifier {
    UINib* nib = [UINib nibWithNibName:reuseIdentifier bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:reuseIdentifier];
    return nib;
}

- (id)initWithRows:(NSArray*) rows andView:(UIView*)view {
    if (self = [super init]) {
        self.rows = rows;
        self.tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
        self.nib = [self getAndRegisterNib:@"PrototypeCell"];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor grayColor];
    }

    return self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrototypeCell" forIndexPath:indexPath];

    MyDataTableEntry* entry = self.rows[indexPath.row];

    [cell.button setTitle:entry.name forState:UIControlStateNormal];
    cell.button.backgroundColor = [UIColor blueColor];

    return cell;
}

@end

Here is a snippet that creates the table:

MyDataTableEntry* entry = [[MyDataTableEntry alloc] init];
entry.title = @"hello";
self.dataTable = [[MyDataTable alloc] initWithRows:@[entry] andView:self.view];
[self.view addSubview:self.dataTable.tableView];
[self.dataTable.tableView setNeedsLayout];
[self.dataTable.tableView layoutIfNeeded];
[self.dataTable.tableView reloadData];

Upvotes: 0

Views: 56

Answers (1)

drekka
drekka

Reputation: 21883

Without confirming in a debugger:

The problem is here:

self.dataTable = [[MyDataTable alloc] initWithRows:@[entry] andView:self.view];
[self.view addSubview:self.dataTable];

self.dataTable is an NSObject. The second line should read something like this:

[self.view addSubview:self.dataTable.tableView];

I would also suggest that you engage in some refactoring. MyDataTable is not a view. It's a controller. So rename to MyDataTableController. Then the bug would have been more obvious.

I would also ask why you are doing this in code? you are already using nibs. So why not go to storyboards. Doing that and using UITableViewController as a base class should enable you to remove a lot of the code and potential bugs.

Upvotes: 3

Related Questions