如何在UITableView中为UICollectionViewCell使用相同的代码?

时间:2021-07-24 08:01:08

I'm using the exact same cell setup from a UICollectionView but this time inside of a UITableView, the reason for this is that i'm using collapsable headers so it needs to be a UITableView. The code will be pretty much exactly the same apart from the type will be a UITableViewCell instead of a UICollectionViewCell and the xib files will be different. Could i somehow make this code re-usable for a UITableViewCell instead of copy-paste?

我正在使用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)
}
}

1 个解决方案

#1


0  

You don't have to use UITableView. You can create sticky headers in UICollectionView with a custom UICollectionViewLayout: https://cocoacasts.com/how-to-add-sticky-section-headers-to-a-collection-view/

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

And you can create expanding headers in CollectionView as well: UICollectionView Header change height in IBAction

您还可以在CollectionView中创建扩展标头:UICollectionView标头在IBAction中更改高度

#1


0  

You don't have to use UITableView. You can create sticky headers in UICollectionView with a custom UICollectionViewLayout: https://cocoacasts.com/how-to-add-sticky-section-headers-to-a-collection-view/

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

And you can create expanding headers in CollectionView as well: UICollectionView Header change height in IBAction

您还可以在CollectionView中创建扩展标头:UICollectionView标头在IBAction中更改高度