首页 > 解决方案 > 错误:无法使用自定义单元格类设置 UITableViewCell 标签和图像!只能加载单元格样式的数据:字幕不是自定义的

问题描述

在过去的 2 周里,我一直在努力让我的 iphone 应用程序与自定义 UITableViewCell 类一起工作,并且无法使用自定义类在 tableview 单元格中设置标签或图像。我已经能够使用标准单元格类型的字幕为每个单元格设置 textLabel 和 detailTextLabel 以及图像。

当我尝试在界面构建器中创建自定义单元格时,我转到单元格属性选择自定义作为单元格样式并为其指定“单元格”标识符。在代码中,我创建了一个名为class DrinkListTableViewController: UITableViewController. 在同一个文件中,我创建了一个名为的自定义单元格类class DrinkTableViewCell: UITableViewCell以及一个名为struct Drink. 我已经验证了来自我的 api 调用的数据是好的并且按预期加载,但是我只能在cell.imageView?.image = cell.textLabel?.text = cell.detailTextLabel?.text =我真的想使用我创建的自定义单元格类时设置单元格标签和图像(请参阅代码作为参考)。

import Foundation
import UIKit
import Alamofire

struct Drink {
    let id: String
    let name: String
    let description: String
    let amount: Float
    let image: UIImage

    init(data: [String: Any]) {
        self.id = data["id"] as! String
        self.name = data["name"] as! String
        //self.amount = data["amount"] as! Float
        self.amount = ((data["amount"] as? NSNumber)?.floatValue)!
        self.description = data["description"] as! String
        self.image = data["image"] as! UIImage
    }
}

class DrinkTableViewCell: UITableViewCell {
    @IBOutlet weak var cellName: UILabel!
    @IBOutlet weak var cellAmount: UILabel!
    @IBOutlet weak var cellDescription: UILabel!
    @IBOutlet weak var cellImage: UIImageView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class DrinkListTableViewController: UITableViewController {

    var drinks: [Drink] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Drink Selection"
        tableView.dataSource = self
        tableView.delegate = self
        //tableView.register(DrinkTableViewCell.self, forCellReuseIdentifier: "cell")

        tableView.register(DrinkTableViewCell.self as AnyClass, forCellReuseIdentifier: "cell")

        //tableView.register(UINib(nibName: "DrinkTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")

        //tableView.estimatedRowHeight = 134
        //tableView.rowHeight = UITableView.automaticDimension

        fetchInventory { drinks in
            guard drinks != nil else { return }
            self.drinks = drinks!
            //print("Data from API call: ", self.drinks)
            //self.tableView.reloadData()
//            DispatchQueue.main.async { [weak self] in
//                self?.tableView.reloadData()
//            }
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.async { [weak self] in
            self?.tableView.reloadData()
        }
    }


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "pizzaSegue", sender: self.drinks[indexPath.row] as Drink)
        //trying another method below?
        //self.navigationController?.pushViewController(UIViewController() as! PizzaViewController, animated: true)
    }



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "pizzaSegue" {
            guard let vc = segue.destination as? PizzaViewController else { return }
            vc.pizza = sender as? Pizza
        }
    }

    private func fetchInventory(completion: @escaping ([Drink]?) -> Void) {
Alamofire.request("http://127.0.0.1:4000/inventory", method: .get)
        .validate()
        .responseJSON { response in
            guard response.result.isSuccess else { return completion(nil) }
            guard let rawInventory = response.result.value as? [[String: Any]?] else { return completion(nil) }
            let inventory = rawInventory.compactMap { pizzaDict -> Drink? in
                var data = pizzaDict!
                data["image"] = UIImage(named: pizzaDict!["image"] as! String)
                //print("Printing each item: ", Drink(data: data))
                //printing all inventory successful
                return Drink(data: data)
            }
            completion(inventory)
    }
}

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("ROWS: ", drinks.count)
        return drinks.count
    }


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

        //let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DrinkTableViewCell

        //let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")

        let cell:DrinkTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! DrinkTableViewCell

        cell.cellName?.text = drinks[indexPath.row].name
        cell.cellAmount?.text = String(drinks[indexPath.row].amount)
        cell.cellDescription?.text = drinks[indexPath.row].description
        cell.cellImage?.image = drinks[indexPath.row].image

//        cell.imageView?.image = drinks[indexPath.row].image
//        cell.textLabel?.text = drinks[indexPath.row].name
//        cell.detailTextLabel?.text = drinks[indexPath.row].description
//
        //print(cell.textLabel?.text)
        //print(cell.detailTextLabel?.text)

        print(cell.cellName?.text as Any)
        //print(cell.cellImage?.image)
        return cell
    }

     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     return 100.0
     }

}

当使用自定义类设置单元格标签和图像时,应用程序不会崩溃,cell.cellName?.text = drinks[indexPath.row].name但 tableView 不会加载任何内容,如在 tableView 中具有来自drinks.count 的部分数量的空白tableView 覆盖func numberOfRowsInSection。请帮忙!

标签: arraysswiftxcodeuitableview

解决方案


我认为这是因为您需要使用:

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

在您的代码中被注释掉,而不是:

    let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")

如果这不起作用,请尝试将其添加到您的自定义单元类中,如果它不起作用,这可能就是您最初将其注释掉的原因:

 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

编辑:

让我们尝试不使用情节提要。

class PostCell: UITableViewCell {

//Declare items

let cellName = UILabel()
let cellAmount = UILabel()
let cellDescription = UILabel()
let cellImage = UIImageView()

//Setup the layouts of your items

func configureCustomCell() {

    contentView.backgroundColor = .clear

    contentView.addSubview(cellName)
    cellName.translatesAutoresizingMaskIntoConstraints = false
    cellName.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 25).isActive = true
    cellName.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true

    contentView.addSubview(cellAmount)
    cellAmount.translatesAutoresizingMaskIntoConstraints = false
    cellAmount.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 25).isActive = true
    cellAmount.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true

    contentView.addSubview(cellDescription)
    cellDescription.translatesAutoresizingMaskIntoConstraints = false
    cellDescription.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
    cellDescription.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true

    contentView.addSubview(cellImage)
    cellImage.translatesAutoresizingMaskIntoConstraints = false
    cellImage.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -5).isActive = true
    cellImage.widthAnchor.constraint(equalToConstant: 50).isActive = true
    cellImage.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true

}

 //Get ready for use

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

然后确保你有'viewDidLoad':

tableView.register(DrinkTableViewCell.self as AnyClass, forCellReuseIdentifier: "DTVCell")

然后在' override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell' 我们应该可以做到:

let cell:UserMessagesCell = self.tableView.dequeueReusableCell(withIdentifier: "DTVCell") as! DrinkTableViewCell

并使用:

cell.cellName.text = "name"

编辑:

有时单元格行高的大小会影响自定义单元格。尝试增加每行的高度以确保一切都适合,因为有时如果高度错误,您可能会得到空单元格。

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

尝试使用 if 语句来避免 API 调用出现任何问题。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.drinks.count == 0 {
        print("No Data Yet: ", drinks.count)
        return 0
    } else {
           print("ROWS: ", drinks.count)
           return self.drinks.count
    }
}

推荐阅读