I'm trying to create a custom keyboard, showing a list of choices.
I've created a xib-file (based on UIView
) which only contains a UITableView
.
I've created ListKeyBoardView
.h and ListKeyBoardView.m
(see code below). In ListKeyBoardView.m
I load the nib-file and the UITableView
from the xib is hooked up to the UITableView ithrough Interface Builder. After loading the nib-file I've checked the frame size of the UITableView
. It is the same size as the UITableView
in Interface Builder, so it seems to be hooked up correctly. However when I run the app and the view is shown, it is completely blank.
I've set the delegate and datasource for the UITableView
in the code and the method tableView:numberOfRowsInSection:
is called (returning 6), but tableView:cellForRowAtIndexPath:
is not called.
To check for other errors I created the UITableView
manually in the code (see commented line) and then it is working fine. What I am missing?
#import "ListKeyBoardView.h"
@interface ListKeyBoardView () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (strong, nonatomic) NSMutableArray *listData;
@end
@implementation ListKeyBoardView
- (id)init {
return [self initWithFrame:CGRectMake(0, 0, 320, 250)];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"ListKeyboard" owner:self options:nil];
// self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
NSLog(@"Frame = %f, %f", self.listTableView.frame.size.width, self.listTableView.frame.size.height);
[self addSubview:self.listTableView];
self.listTableView.delegate = self;
self.listTableView.dataSource = self;
[self.listTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ListItem"];
self.listData = [[NSMutableArray alloc] init];
[self.listData addObject:@"een"];
[self.listData addObject:@"twee"];
[self.listData addObject:@"drie"];
[self.listData addObject:@"vier"];
[self.listData addObject:@"vijf"];
[self.listData addObject:@"zes"];
}
return self;
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ListItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [self.listData objectAtIndex:indexPath.row];
return cell;
}
@end