How do you load custom UITableViewCells from Xib files?

前端 未结 23 2157
抹茶落季
抹茶落季 2020-11-22 11:11

The question is simple: How do you load custom UITableViewCell from Xib files? Doing so allows you to use Interface Builder to design your cells. The answer app

23条回答
  •  星月不相逢
    2020-11-22 11:27

    1. Create your own customized class AbcViewCell subclass from UITableViewCell (Make sure your class file name and nib file name are the same)

    2. Create this extension class method.

      extension UITableViewCell {
          class func fromNib() -> T {
              return Bundle.main.loadNibNamed(String(describing: T.self), owner: nil, options: nil)?[0] as! T
          }
      }
      
    3. Use it.

      let cell: AbcViewCell = UITableViewCell.fromNib()

提交回复
热议问题