ios - 注册 TableView 单元格时在 Swift 中使用 AnyClass

标签 ios swift uitableview

在尝试注册 TableView 单元格时,我在 Swift 中使用 AnyClass 时遇到了问题。

例子:

self.tableView.register(cellClass: AnyClass?, forCellReuseIdentifier: String)

self.tableView.register(UITableViewCell.self(NSObject), forCellReuseIdentifier: <String>)

当我传入 UITableViewCell(或其子类)并对其调用“self”时,我需要传入一个额外的“NSObject”参数。

到目前为止,我在网上看到的是 Class.self 而不是 Class.self(NSObject)

请告知 - 这是新的 Swift API 更改吗?

最佳答案

你可以注册 Nib 名称,它会加载类

 tableView.register(UINib(nibName: "mainTableViewCell", bundle: nil), forCellReuseIdentifier: CellIdentifier1)

tableView.register(mainTableViewCell.self, forCellReuseIdentifier: CellIdentifier1)

关于ios - 注册 TableView 单元格时在 Swift 中使用 AnyClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48606602/

相关文章:

iphone - NSDateComponents包装在结构中?

ios - 如何在 swift 中使用 MR 从 coreData 获取数据

swift - 我无法使用 UIButton 从 UIViewController 到 UITableViewController

ios - 执行segue不工作

ios - 无法理解 fetchRequest.predicate

javascript - iphone 4 链接创建红色半透明背景/触摸覆盖 :active

ios - 有没有办法查看文本字段的值是否等于 Swift 中字典中的值?

ios - 符合 Swift 中的 UITableViewDelegate 和 UITableViewDatasource 吗?

iphone - UITableViewCell 内不显示 UILabel

ios - 当键盘快速出现时向上滚动 UITableView