简体   繁体   中英

embed custom view into UITableViewCell

I already have a subclass of UIView , called CustomView , which I use in stack views.

I am wanting to redo the UI and use a table view instead. So, for that I just wanted to reuse the CustomView class I already have but for some reason the table view is not showing the cell at all.

I think the issue is the way I am using CustomView in CustomCell: UITableViewCell :

class CustomCell: UITableViewCell {
  @IBOutlet var customView: CustomView!
}

in the view controller holding the table view, I have in viewDidLoad :

    tableView.register(UINib(nibName: "CustomCell", bundle: Bundle.main), forCellReuseIdentifier: "CustomCell")

and

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
  {
    return tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
  }

When I run the app, I just get a blank table view. 在此处输入图片说明

Good day I made the same code that you, and works , could you add breakpoint in this position.

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
  {
    return tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
  }

you shouldn't have problem using custom views inside cells.

您必须为自定义视图使用指定的初始化程序,如 init(frame:CGRect) 或将其添加到界面构建器中,如原型单元格。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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