0

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
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
Leontien
  • 612
  • 5
  • 22

2 Answers2

0

You've forgotten to implement -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

Do it like this:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
iCode
  • 1,456
  • 1
  • 15
  • 26
  • numberOfSectionsInTableView: is optional. To be sure I implemented it anyway and the result is the same. – Leontien Oct 14 '13 at 18:07
0

The problem here is that the tableView is created in storyboard and thus initWithCoder is called and not initWithFrame.

To solve, override initWithCoder rather than initWithFrame and do not override the init method to call initWithFrame.

Carien van Zyl
  • 2,853
  • 22
  • 30