【问题标题】:uitableviewcell not showing swift 3uitableviewcell 不显示 swift 3
【发布时间】:2017-12-08 04:10:06
【问题描述】:

我正在处理有关表格视图控制器的快速任务,但我的表格视图上没有任何内容。我已正确连接标签和类,并在情节提要的属性中将 tableview 单元格标识符分配为“单元格”,但我仍然有错误“需要注册笔尖”,即使我写了self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell"),错误消失了,但仍然没有就可以了。

我的单元格如下。

class cartTableViewCell: UITableViewCell {

@IBOutlet weak var itemImage: UIImageView!
@IBOutlet weak var itemName: UILabel!
@IBOutlet weak var itemQuan: UILabel!
@IBOutlet weak var itemFrame: UILabel!
@IBOutlet weak var itemSize: UILabel!
@IBOutlet weak var itemPrice: UILabel!
@IBOutlet weak var itemSub: UILabel!

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}`

我的功能是

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

    let item = itemArray[indexPath.row]

    cell.itemName?.text = item.itemName!
    cell.itemFrame?.text = "haha"//itemArray[indexPath.row].itemFrame!
    cell.itemSize?.text = item.itemSize!
    cell.itemPrice?.text = item.sellingPrice!
    //cell.itemQuan?.text =
    cell.itemSub?.text = "\(Int(item.sellingNumber!)! * Int(item.sellingPrice!)!)"
    return cell
}`

如果我打印(item.itemName),我可以获得一些价值。 cell.itemName?.text 为零

我在网上查了一些答案,但到目前为止还没有运气。非常感谢任何帮助。

【问题讨论】:

    标签: uitableview swift3


    【解决方案1】:

    删除代码

    self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
    

    在cartTableViewCell中写下代码

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
                super.init(style: style, reuseIdentifier: reuseIdentifier)
                // code common to all your cells goes here
            }
    
            required init?(coder aDecoder: NSCoder) {
                super.init(coder: aDecoder)
            }
    

    现在用下面的代码修改你的代码

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = self.tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath) as! cartTableViewCell
        if cell == nil {
                cell = cartTableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
            }
        let item = itemArray[indexPath.row]
    
        cell.itemName?.text = item.itemName!
        cell.itemFrame?.text = "haha"//itemArray[indexPath.row].itemFrame!
        cell.itemSize?.text = item.itemSize!
        cell.itemPrice?.text = item.sellingPrice!
        //cell.itemQuan?.text =
        cell.itemSub?.text = "\(Int(item.sellingNumber!)! * Int(item.sellingPrice!)!)"
        return cell
    }
    

    【讨论】:

    • @Irene Z 声明类名时以大写字母开头
    【解决方案2】:

    只需检查代码中的重用标识符名称。我只是为了更多解释而写这篇文章。

    viewDidLoad(){
                let customNib = UINib(nibName: "CustomCell", bundle: Bundle.main)
                tableView.register(customNib, forCellReuseIdentifier: "customCell")
            }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
       cell.configureCell(text: data[indexPath.row])
    }
    

    永远不要这样做:

    let cell = self.tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath) as! cartTableViewCell
            if cell == nil {
                    cell = cartTableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
                }
    

    解释:

    具有关联重用标识符的 UITableViewCell 对象。这 方法总是返回一个有效的单元格。参考:https://developer.apple.com/documentation/uikit/uitableview/1614878-dequeuereusablecell.

    【讨论】:

      【解决方案3】:

      执行此操作,我今天刚刚收到此错误,我已通过以下方式修复它:

      let yourNib = UINib(nibName: "yourNibName", bundle: Bundle.main)
      yourTableView.register(yourNib, forCellReuseIdentifier: "cell")
      

      【讨论】:

        猜你喜欢
        • 2015-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多