简体   繁体   English

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

[英]Displaying custom cell label text in a minimized view controller

I'm minimizing my view controller, so it only deals with user interactions, and leaves data handling and manipulation to a separate data model class.我正在最小化我的视图控制器,所以它只处理用户交互,并将数据处理和操作留给单独的数据模型类。

I don't get any custom cell label text values being displayed when I run the app.运行应用程序时,没有显示任何自定义单元格标签文本值。 My cellForRowAt method is:我的 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
    }

and my custom cell class is:我的自定义单元类是:

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")
    }
}

I've also made a protocol that allows cell reuse, without having to hard code the cell reuse identifier in the view controller.我还制定了一个允许单元重用的协议,而不必在视图控制器中硬编码单元重用标识符。 Just to make new view controllers easier and simpler to add as the app develops in the future.只是为了让新的视图控制器在未来应用程序开发时更容易和更简单地添加。

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
    }
}

I'v done all the usual registering table view custom cells (as you can see in the view controller viewDidLoad), and I have all storyboard interface builder connections made.我已经完成了所有常用的注册表格视图自定义单元格(正如您在视图控制器 viewDidLoad 中看到的那样),并且我已经建立了所有故事板界面构建器连接。 It worked before I started view controller minimization, and I haven't changed any storyboard options since it worked.它在我开始视图控制器最小化之前就起作用了,并且自从它起作用以来我没有改变任何故事板选项。

Solution after reviewing code:查看代码后的解决方案:

When registering the WordCustomCell, you should specify the Nib name (WordsTableCell) as the cell is being loaded from the Nib.注册 WordCustomCell 时,您应该指定 Nib 名称 (WordsTableCell),因为从 Nib 加载单元格。

In addition, you need to call super.init on the required init?(coder aDecoder: NSCoder)另外,需要在需要的init上调用super.init?(coder aDecoder: NSCoder)

Find below the changes that you need to make to your code.在下面找到您需要对代码进行的更改。

WordsViewController.swift WordsViewController.swift

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

WordCustomCell.swift WordCustomCell.swift

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

显示单元格文本的应用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文本从单元格移动到另一个视图控制器中的文本标签 - Move Text from cell to text label in a different view controller uiTableview单元格未显示在视图控制器中 - uiTableview cell is not displaying in view controller 在第一个视图控制器上的标签中显示输入到文本字段(在第二个视图控制器中)的文本? - Displaying text entered into textfield (in second view controller) in a label on first view controller? 自定义标签不显示在自定义单元格中 故事板 - Custom label not displaying in custom cell. Storyboard 如何使用表格视图单元格中的文本并将其显示为父视图控制器中的标签? - How can I use text from a table view cell and display it as a label in a parent view controller? 如何根据表视图单元格的详细文本标签,将segue设置为与表视图控制器不同的视图控制器? - How can I set a segue to a different view controller from a table view controller, based on the detail text label of the table view cell? 将单元格标签或标题传递给另一个视图控制器 - Passing cell label or title to another view controller 在我的主视图控制器中获取自定义单元格中用于heightForRowAt的标签的高度 - getting the height of a label in custom cell to use for heightForRowAt in my main view controller 将信息从tableview传递给视图控制器而不是基于indexPathForSelectedRow,而是传递给选定单元格的标签文本? - Pass information from a tableview to a view controller based not on indexPathForSelectedRow, but on label text of cell selected? 在拆分视图中将自定义标签添加到原型单元格 - Adding a custom label to a prototype cell in a split view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM