简体   繁体   English

您如何为表格视图单元格制作和使用 XIB 文件,而不是仅在故事板的表格视图中使用单元格?

[英]How do you make and use an XIB file for a table view cell, rather than just using a cell "in" the table view in storyboard?

In a normal storyboard, table views come "with" embedded cells.在普通的故事板中,表格视图“带有”嵌入的单元格。 Simply set "Dynamic Prototypes" to 1 (or more):只需将“动态原型”设置为 1(或更多):

在此处输入图片说明

You then use the those cells in your view controller simply with然后,您只需使用视图控制器中的这些单元格

...cellForRowAt

  let cell = tableView.dequeueReusableCell(
     withIdentifier:"YourCellClassID", for: indexPath) as! YourCellClass

Where YourCellClass is the class of the table view cell,其中YourCellClass是表格视图单元格的类,

and the string "YourCellClassID" is just而字符串“YourCellClassID”就是

the "Identifier" set in the Attributes Inspector (nowadays the **5th button)** of the storyboard.在故事板的属性检查器(现在是 **5th按钮)** 中设置的“标识符”

(Be careful to NOT use the "restoration identifier" on the 4th button, identity inspector, which sounds the same but is unrelated.) (注意不要使用第 4 个按钮上的“恢复标识符”,身份检查器,这听起来相同但不相关。)

在此处输入图片说明

But what if you want to use an XIB file ?但是如果您想使用 XIB 文件怎么办?

Instead of using one of the prototypes "in" the table view in storyboard?而不是在故事板的表格视图中“使用”原型之一?

If you use an XIB file, you can use the same cell in different tables .如果使用 XIB 文件,则可以在不同的表中使用相同的单元格

How to do it?怎么做?

Step 1, to make a cell-style XIB file第一步,制作单元格样式的XIB文件

In current (2020) Xcode there's no direct button to create a table view cell -style XIB file.在当前(2020 年)Xcode 中,没有直接按钮来创建表格视图单元格样式的 XIB 文件。

The secret is秘密是

  1. create a 'Cocoa Touch Class', select UITableViewCell创建一个“Cocoa Touch Class”,选择 UITableViewCell

  2. and ALSO select 'create XIB file'并选择“创建 XIB 文件”

在此处输入图片说明

在此处输入图片说明

You now have a cell-style XIB file, TesteCell.xib in the example.您现在有一个单元格样式的 XIB 文件,示例中的 TesteCell.xib。

(Feel free to delete TesteCell.swift if you don't need it.) (如果不需要,请随意删除 TesteCell.swift 。)

Step 2, add the register#UINib code in viewDidLoad第二步,在viewDidLoad添加register#UINib代码

If you now try this:如果你现在试试这个:

let cell = tableView.dequeueReusableCell(
              withIdentifier: "TesteCellID",
              for: indexPath) as! TesteCell

In fact it does not work.其实这行不通的。

In viewDidLoad , you must addviewDidLoad ,您必须添加

override func viewDidLoad() {
    super.viewDidLoad()
    // We will be using an XIB file, rather than
    // just a cell "in" the table view on storyboard
    tableView.register(
        UINib(nibName: "filename without suffix", bundle: nil),
        forCellReuseIdentifier: "identifier in attributes inspector"
    )
}

In the example在示例中

override func viewDidLoad() {
    super.viewDidLoad()
    // We will be using an XIB file...
    tableView.register(
        UINib(nibName: "TesteCell", bundle: nil),
        forCellReuseIdentifier: "TesteCellID")
}

描述

And that's how you do it.这就是你的方式。

Summary概括

  1. New file - cocoa touch class - subclass of table cell - do select 'also create XIB file'新文件 - 可可触摸类 - 表格单元的子类 - 选择“同时创建 XIB 文件”

  2. In viewDidLoaed, register the XIB file using,在 viewDidLoaed 中,使用以下命令注册 XIB 文件,

  3. The file name (no suffix) and the identifier文件名(无后缀)和标识符

  4. You're done, you can now refer to the cell in the normal way in dequeueReusableCell大功告成,现在可以在dequeueReusableCell中以正常方式引用单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM