uitableviewcell没有显示swift 3

时间:2017-12-08 04:10:07

标签: uitableview swift3

我正在进行关于表视图控制器的快速分配,而我的表视图上没有任何内容。我已正确连接标签和类,并在storyboard的属性中将tableview单元标识符指定为“cell”但我仍然有错误“需要注册nib”,即使我写了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为零

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

3 个答案:

答案 0 :(得分:0)

删除代码

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
}

答案 1 :(得分:0)

只需检查代码中的重用标识符名称即可。我只是写这个以获得更多解释。

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

答案 2 :(得分:0)

这样做,我今天刚收到这个错误,我得到了修正:

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