ios - 将 UITableView 添加到 iOS 8 自定义键盘

标签 ios uitableview swift ios-app-extension custom-keyboard

我正在尝试添加 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()

然后我实现了numberOfSectionsInTableViewnumberOfRowsInSection ,在 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/

相关文章:

ios - 仅导入已更新的 XML

ios - 禁用 iOS 7 应用程序的横向方向?

ios - 第一次加载 View 时不填充 TableView (Swift2 和解析)

ios - 如何从 UItableview 中删除单元格,当它在 onChildRemoved 方法中从 Firebase 中删除时?

swift - 多个命令产生

swift - 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因 : 'Failed to set posix_spawn_file_actions for fd -1 at index 0

ios - 谷歌地图 - 获取给定半径内的所有标记

iOS 6 MPMoviePlayerViewController 和 presentMoviePlayerViewControllerAnimated 旋转

ios - (self.nsmutablearray)[indexPath.row] 是什么意思?

ios - 当具有动态单元格高度的 UITableview 放置在里面时,如何扩展/缩小 UICollectionViewCell 高度?