【发布时间】:2013-10-14 17:45:07
【问题描述】:
我正在尝试创建一个自定义键盘,显示一个选项列表。
我创建了一个 xib 文件(基于 UIView),其中仅包含一个 UITableView。
我已经创建了ListKeyBoardView.h 和ListKeyBoardView.m(参见下面的代码)。在ListKeyBoardView.m 中,我加载了nib 文件,并且来自xib 的UITableView 连接到了UITableView ithrough Interface Builder。加载 nib 文件后,我检查了 UITableView 的帧大小。它与 Interface Builder 中的 UITableView 大小相同,因此似乎连接正确。但是,当我运行应用程序并显示视图时,它完全是空白的。
我在代码中为UITableView设置了委托和数据源,调用了tableView:numberOfRowsInSection:方法(返回6),但没有调用tableView:cellForRowAtIndexPath:。
为了检查其他错误,我在代码中手动创建了UITableView(参见注释行),然后它工作正常。我错过了什么?
#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
【问题讨论】:
标签: ios xcode uitableview xib nib