错误:在 swift 中使用 xib 文件注册自定义 tableView 单元格

Posted

技术标签:

【中文标题】错误:在 swift 中使用 xib 文件注册自定义 tableView 单元格【英文标题】:ERROR : registering a custom tableView cell in swift with an xib file 【发布时间】:2018-05-26 10:22:14 【问题描述】:

我检查了所有标识符名称,还检查了其类的名称和所有内容。以下是我的代码和错误。

class StoreInfoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 


var store : StoreInfo = StoreInfo()

@IBOutlet weak var storeInfoTable: UITableView!


override func viewDidLoad() 
    super.viewDidLoad()

    storeInfoTable.register(UINib(nibName: "CustomStoreInfoTableOneViewCell", bundle: nil), forCellWithReuseIdentifier: "storeinfocell1")

    storeInfoTable.delegate = self
    storeInfoTable.dataSource = self




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


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = storeInfoTable.dequeueReusableCell(withIdentifier: "storeinfocell1", for: indexPath) as! CustomStoreInfoTableOneViewCell

    cell.storeInfoImage.sd_setImage(with: URL(string: (store.imageurl)), placeholderImage: UIImage(named: "r1"), options: [.continueInBackground, .progressiveDownload], completed: nil)

    cell.storeinfoName.text = store.storename


Here's the error.

【问题讨论】:

Custom UITableViewCell from nib in Swift的可能重复 【参考方案1】:

你写错了syntax

  self. storeInfoTable.register(UINib(nibName: "CustomStoreInfoTableOneViewCell", bundle: nil), forCellReuseIdentifier: "storeinfocell1")

【讨论】:

【参考方案2】:

您已在( :forCellWithReuseIdentifier) 注册您的笔尖,但您需要向( : forCellReuseIdentifier) 注册您的笔尖。请用这个更改您的注册行。

storeInfoTable.register(UINib(nibName: "CustomStoreInfoTableOneViewCell", bundle: nil), forCellReuseIdentifier: "storeinfocell1")

【讨论】:

你能解释一下吗? (:forCellWithReuseIdentifier) 用于注册collection view cell

以上是关于错误:在 swift 中使用 xib 文件注册自定义 tableView 单元格的主要内容,如果未能解决你的问题,请参考以下文章

Swift segue 以编程方式转至 xib 文件

swift ios中的自定义谷歌地图标记图标图像

如何使用MVVM + Swift4实现注册屏幕

iOS8 Swift 无法使用 xib 创建视图控制器

我在 swift 3 中有一个错误 [关闭]

使用 Swift 在 Xcode 6 中使用 xib 在自定义 uiview 中获取 SIGABRT