在构建 cell 时,可以将 cell 的识别码设置为其自定义类别的名称。目的是可以清楚的指定识别码并取出Cell,如下所示:

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


如果使用 String(describing: RestaurantDetailIconTextCell.self), 则会回传类别的名称(也就是 「RestaurantDetailIconTextCell 」)

let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RestaurantDetailIconTextCell.self), for: indexPath) as! RestaurantDetailIconTextCell

那这两种方法哪种好呢?

用第一段代码,是将 cell 的识别码固定写入。如果将识别码打错,Xcode 编译也不会出现任何错误。只有在运行 App 的时候才会发现出现问题。

使用第二段代码,如果打错了类别的名称(比如打成 RestaurantDetailIconTxtCell.self ),Xcode 会马上提示错误。所以更加推荐使用第二种代码。