【问题标题】:UITableView cell reuse identifierUITableView 单元重用标识符
【发布时间】:2017-07-19 10:48:41
【问题描述】:

我已经为不同的单元格标识符创建了一个结构:

enum CustomCellIdentifiers
{
    static let cellForCountry = "cellForCountry"
    static let cellForCity = "cellForCity"
    static let cellForStoreType = "cellForStoreType"
}

并且我正在按照 switch case 将单元格注册到表格中,例如:

view.tableForItems.register(UINib.init(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier:cellIdentifier)

但是当我注册单元格时,我得到了reuseIdentifier 为 nil 的错误:

 class CustomTableCell: UITableViewCell
    {
    override func awakeFromNib()
        {
            super.awakeFromNib()
            // Initialization code
            switch self.reuseIdentifier ?? "cellForCountry" //It only work with country cell
{
            case CustomCellIdentifiers.cellForCountry:
                 print("cellForCountry") break;
            case CustomCellIdentifiers.cellForCity:
                 print("cellForCity")                break;
            case CustomCellIdentifiers.cellForStoreType:
                                print("cellForStoreType")
                break;
            default: break

            }
        }
    }

【问题讨论】:

  • 如果您想存储简单的字符串并希望它是不可变的,请使用 enum 而不是 Struct。此外,开关盒中也不需要中断。
  • 使用枚举代替struct来存储值。
  • @Jaydeep 我改变了,但重用标识符的问题是 nil

标签: swift uitableview


【解决方案1】:

xib 文件中也设置reUseIdentifier。 如果您无法在 xib 类中设置重用标识符,则意味着您从 UIView 创建了 xib 文件。反而 以 UITableviewcell 类创建 xib 而不是 UIView.. 那么你就可以从xib文件中分配reuseIdentifier

请看下图

【讨论】:

  • 所以在 switchCase switch self.reuseIdentifier 中没有一个案例被执行?
  • 它是如何与 optional 一起工作的,你能分享一些细节吗?
  • 你在xib文件中设置了reuseIdentifier吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 2019-07-02
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
相关资源
最近更新 更多