gpt4 book ai didi

ios - 在最小化 View Controller 中显示自定义单元格标签文本

转载 作者:行者123 更新时间:2023-12-01 19:34:16 26 4
gpt4 key购买 nike

我正在最小化我的 View Controller ,因此它只处理用户交互,并将数据处理和操作留给单独的数据模型类。

运行应用程序时,我没有显示任何自定义单元格标签文本值。我的 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 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")
}
}

我还制定了一个允许单元重用的协议(protocol),而无需在 View Controller 中对单元重用标识符进行硬编码。只是为了让新的 View Controller 更容易和更简单地随着应用程序的 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
}
}

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

最佳答案

查看代码后的解决方案:
注册 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)
}
App showing cell text

关于ios - 在最小化 View Controller 中显示自定义单元格标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com