gpt4 book ai didi

swift - 我如何在 UITableView 内的 UICollectionViewCell 使用相同的代码?

转载 作者:行者123 更新时间:2023-11-30 12:06:15 26 4
gpt4 key购买 nike

我使用的是 UICollectionView 中完全相同的单元格设置,但这次是在 UITableView 内,原因是我使用的是可折叠标题,因此它需要是 UITableView。除了类型是 UITableViewCell 而不是 UICollectionViewCell 并且 xib 文件会有所不同之外,代码几乎完全相同。我可以以某种方式使此代码可重复用于 UITableViewCell 而不是复制粘贴吗?

class LoggedExerciseCell: UICollectionViewCell {

// MARK: - IBOutlets
@IBOutlet var cell: UICollectionViewCell!
@IBOutlet var loggedSetTableView: LoggedSetsTableView!
@IBOutlet weak var exerciseName: UILabel!
@IBOutlet weak var exerciseCount: UILabel!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var resistanceType: UILabel!
@IBOutlet weak var repetitionType: UILabel!

// MARK: - Object Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

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

// MARK: - Configure Cell
func configure(_ exercise: LoggedExerciseViewModelView) {
self.exerciseName.text = exercise.exerciseName
self.icon.image = UIImage(named: exercise.icon)
self.resistanceType.text = exercise.resistanceType
self.repetitionType.text = exercise.repetitionType
self.loggedSetTableView.loggedExerciseViewModel = exercise
}
}

// MARK: - CommonInit
private extension LoggedExerciseCell {
func commonInit() {
Bundle.main.loadNibNamed("LoggedExerciseCell", owner: self, options: nil)
cell.frame = self.bounds
addSubview(cell)
configureViews()
}
}

// MARK: - ConfigureViews
private extension LoggedExerciseCell {
func configureViews() {
configureIconImageView()
}

func configureIconImageView() {
icon.setCircularImageViewWithBorder(borderWidth: 1.2, withBorderColor: UIColor.darkBlue().cgColor)
}
}

最佳答案

您不必使用 UITableView。您可以使用自定义 UICollectionViewLayout 在 UICollectionView 中创建粘性 header :https://cocoacasts.com/how-to-add-sticky-section-headers-to-a-collection-view/

您也可以在 CollectionView 中创建扩展标题: UICollectionView Header change height in IBAction

关于swift - 我如何在 UITableView 内的 UICollectionViewCell 使用相同的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640539/

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