我正在尝试添加 UITableView
到 iOS8 中的自定义键盘。由于键盘没有 Storyboard之类的接口(interface)文件,所以我需要添加UITableView
以编程方式,但它不会出现。
在 viewDidLoad
我正在这样做:
self.tableView = UITableView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height), style: UITableViewStyle.Plain)
tableView.delegate = self;
tableView.dataSource = self;
self.view.addSubview(self.tableView)
tableView.reloadData()
然后我实现了
numberOfSectionsInTableView
和 numberOfRowsInSection
,在 cellForRowAtIndexPath
我这样做的方法: var cell = tableView.dequeueReusableCellWithIdentifier("CopyCell", forIndexPath: indexPath) as UITableViewCell
if let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CopyCell", forIndexPath: indexPath) as? UITableViewCell {
cell.textLabel!.text = "Hello World"
cell.backgroundColor = UIColor.redColor()
}else{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CopyCell")
cell.textLabel!.text = "Hello World"
cell.backgroundColor = UIColor.redColor()
}
return cell
我不知道我是否遗漏了一些重要的细节,但是表格 View 不会出现在自定义键盘中,我添加了背景颜色和单元格来指示它。
最佳答案
我能够通过以下方式做到这一点:
1)将tableview添加到XIB文件
2) 将 XIB 的 File 的所有者类分配给 KeyboardViewController
3) 将委托(delegate)和数据源连接到文件的所有者
4) 在您的 KeyboardViewController.m 中为 tableview 创建一个 socket 并将其连接到 XIB 文件中
从那里你必须确保为 tableView 的单元格注册一个类:
[self.tableView registerClass:[UITableView class] forCellReuseIdentifier:@"cell"];
您还需要确保包含此 init 方法:
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:@"Keyboard" bundle:nibBundleOrNil];
return self;
}
从那里您可以正常在 KeyboardViewController 中实现表的委托(delegate)方法,您将看到更改。如果您需要任何帮助,请随时与我们联系,因为过去 2 天我在这方面进行了广泛的工作。
关于ios - 将 UITableView 添加到 iOS 8 自定义键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26368033/