【发布时间】:2019-08-02 11:48:28
【问题描述】:
我在一个普通的 UIViewController 类上使用了一个 tableView (myTableView)。我希望在这个 tableView 中使用可重复使用的单元格来节省内存。我没有创建单独的 XIB 并将 tableView 组件拖放到 viewController 上。
单元格已使用 UITableViewCell 类型的新类 HistoryTableViewCell 创建,并且该类也具有 XIB。
我还创建了一个 tableViewCell 及其 XIB,并在 tableView(_,cellForRowAt:) 方法中使用了以下代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "historyReuseIdentifier", for: indexPath) as! HistoryTableViewCell
cell.meetingDateLabel.text = historyArray![indexPath.section]
return cell
}
当我在viewDidLoad() 方法中添加以下行时,这行代码可以很好地与 tableViewController 一起使用:
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
但tableView.register 属性不能与 tableView 一起使用。我如何利用这里的可重复使用的细胞?
【问题讨论】:
-
提问时请多加努力。不,这不适用于视图控制器或表格视图控制器。您的标识符不匹配。回答你的问题;与使用表格视图控制器的方式完全相同。
-
我认为你使用了不同的 reusableIdentifier.register 和 dequeable cell 有不同的标识符名称
-
如果您只有一个表格视图,请删除 XIB 并在 Interface Builder 的表格视图中设计 所有 单元格
-
@Desdenova 感谢您的建议,问题已更新。我找到了问题的答案。必须使用 _mytableView.register() 而不是使用 tableView.register(),并且代码可以正常工作。