在最小化视图控制器中显示自定义单元格标签文本

Displaying custom cell label text in a minimized view controller

我正在最小化我的视图控制器,因此它只处理用户交互,并将数据处理和操作留给单独的数据模型 class。

我 运行 应用程序时没有显示任何自定义单元格标签文本值。我的 cellForRowAt 方法是:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as WordCustomCell

        let viewModel = WordViewModel(tableViewWord: tableViewWords[indexPath.row], selectedCell: selectedCells[indexPath.row], isEdit: isEditCell)
        cell.setUpWith(viewModel)
        return cell
    }

我的自定义单元格 class 是:

class WordCustomCell: UITableViewCell {

    @IBOutlet weak var englishWord: UILabel?
    @IBOutlet weak var foreignWord: UILabel?
    @IBOutlet weak var wordCheckmark: UIButton?
    @IBOutlet weak var buttonLeft: NSLayoutConstraint?

    var isCellTypeEditing:Bool = false
    var isConstraintUpdated:Bool = false
    let wordVC = WordsViewController()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

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

    func setcellType(cellType:cellType) {
        switch cellType {
        case .cellTypeEditing:
            self.isCellTypeEditing = true
        case .cellTypeNonEditing:
            self.isCellTypeEditing = false

        }
    }

    override func updateConstraints() {
        super.updateConstraints()

        switch isCellTypeEditing {
        case true:
            print("isCellTypeEditing in updateConstraints in LanguageCustomCell is: \(isCellTypeEditing)")
            self.buttonLeft?.constant = 10
        case false:
            print("isCellTypeEditing in updateConstraints in LanguageCustomCell is: \(isCellTypeEditing)")
            self.buttonLeft?.constant = -30
        }
    }

    func setUpWith(_ viewModel: WordViewModel) {
        print("viewModel.englishWordTitle in setUpWith in WordCustomCell is: \(viewModel.englishWordTitle)")
        englishWord?.text = viewModel.englishWordTitle
        print("viewModel.foreignWordTitle in setUpWith in WordCustomCell is: \(viewModel.foreignWordTitle)")
        foreignWord?.text = viewModel.foreignWordTitle
        let selectedCell = viewModel.selectedCell
        let isEditCell = viewModel.isEdit

        if isEditCell == true {
            wordCheckmark?.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.setcellType(cellType: .cellTypeEditing)

                self.setNeedsUpdateConstraints()
                self.wordVC.view.layoutIfNeeded()
                }, completion: { (true) in

            })
            print("if selectedCell in tableView(cellForRowAt) in LanguagesViewcontroller is: \(selectedCell)")
            if selectedCell{
                wordCheckmark?.setImage(UIImage.init(named: "checked"), for: UIControl.State.normal)
                //cell?.layer.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).cgColor
            } else {
                wordCheckmark?.setImage(UIImage.init(named: "unchecked"), for: UIControl.State.normal)
            }
            wordCheckmark?.contentEdgeInsets = UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5)
            foreignWord?.text = ""
            wordVC.enableClear()
        } else {
            wordVC.tableView?.allowsMultipleSelection = false
            wordCheckmark?.isHidden = false
            wordCheckmark?.setImage(UIImage.init(named: "dot"), for: UIControl.State.normal)
            wordCheckmark?.contentEdgeInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
            setcellType(cellType: .cellTypeNonEditing)
            //cell?.layer.backgroundColor = UIColor.white.cgColor
            setNeedsUpdateConstraints()
            wordVC.view.layoutIfNeeded()
        }
    }

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

我还制定了一个允许单元格重用的协议,而无需在视图控制器中对单元格重用标识符进行硬编码。只是为了让新的视图控制器在未来的应用程序开发中更容易和更简单地添加。

import Foundation
import UIKit

//1 protocol declaration
protocol Reusable {}
//2 protocol extension
extension Reusable where Self: UITableViewCell  {
    static var reuseIdentifier: String {
        return String(describing: self)
    }
}
//3 conforming to protocol
extension UITableViewCell: Reusable {}
//4 using generics to make code reusable
extension UITableView {
    func register<T: UITableViewCell>(_ :T.Type) {
        register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
    }

    func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T {
        guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
            fatalError("Could not deqeue cell")
        }
        return cell
    }
}

我已经完成了所有常规注册 table 视图自定义单元格(如您在视图控制器 viewDidLoad 中看到的那样),并且我已经建立了所有故事板界面构建器连接。它在我开始视图控制器最小化之前工作,并且自从它工作以来我没有更改任何故事板选项。

查看代码后的解决方案:

注册 WordCustomCell 时,您应该指定 Nib 名称 (WordsTableCell),因为单元格是从 Nib 加载的。

此外,需要在需要的init上调用super.init?(coder aDecoder: NSCoder)

在下方找到您需要对代码进行的更改。

WordsViewController.swift

// Register custom cell
tableView?.register(UINib(nibName: "WordsTableCell", bundle: nil), forCellReuseIdentifier: "WordCustomCell")
// tableView?.register(WordCustomCell.self)
tableView?.reloadData()

WordCustomCell.swift

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}