我使用的是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。您可以使用自定义 UI 在 UICollectionView 中创建粘性标头 在线视图布局:https://cocoacasts.com/how-to-add-sticky-section-headers-to-a-collection-view/
您也可以在 CollectionView 中创建扩展标题:UICollectionView 标头更改高度 IBAction