[英]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.