gpt4 book ai didi

ios - 从 UICollectionViewController 获取 indexPath 到 UICollectionViewCell 子类

转载 作者:搜寻专家 更新时间:2023-11-01 06:12:17 26 4
gpt4 key购买 nike

在我的 View Controller 中,我正在加载一个带有子类的自定义 CollectionViewCell。根据单元格索引路径的位置,我想以不同方式设置文本标签的格式。 IE。第一行只有一个单元格,文本较大,而第二行有两个单元格,文本较小。

如何从我的 UICollectionViewCell 子类中的 UICollectionView 访问索引路径?我尝试了委托(delegate)协议(protocol),但这总是返回 nil。

下面的代码,非常感谢!

马库斯

UICollectionViewController:

import UIKit

protocol WorkoutDataViewControllerCVDataSource: AnyObject {

func workoutType(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -> WorkoutType
func workoutDistance(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -> Double
func workoutDuration(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -> Double
func workoutInstantVelocity(for workoutDataViewControllerCV: WorkoutDataViewControllerCV) -> Double
}

final class WorkoutDataViewControllerCV: UIViewController {

@IBOutlet weak var collectionView: UICollectionView!

weak var dataSource: WorkoutDataViewControllerCVDataSource!

private lazy var velocityFormatter = VelocityFormatter(dataSource: self, delegate: self)
private lazy var averageVelocityFormatter = VelocityFormatter(dataSource: self, delegate: self)

override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.register(MeasurementCollectionViewCell.preferredNib, forCellWithReuseIdentifier: MeasurementCollectionViewCell.preferredReuseIdentifier)
}

}

// MARK: - Managing UICollectionView

extension WorkoutDataViewControllerCV: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Measurement Cell", for: indexPath)
return cell
}

}

extension WorkoutDataViewControllerCV: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {

let availableWidth = self.view.frame.width

switch indexPath.row {
case 0: return CGSize(width: availableWidth, height: 150)
case 1: return CGSize(width: availableWidth/2.1, height: 150)
case 2: return CGSize(width: availableWidth/2.1, height: 150)
case 3: return CGSize(width: availableWidth, height: 150)
default:
return CGSize(width: availableWidth/2.1, height: 150)
}
}
}

// MARK: - Managing VelocityFormatter

extension WorkoutDataViewControllerCV: VelocityFormatterDataSource {

func duration(for velocityFormatter: VelocityFormatter) -> Double {
return dataSource.workoutDuration(for: self)
}

func distance(for velocityFormatter: VelocityFormatter) -> Double {
return dataSource.workoutDistance(for: self)
}

func instantVelocity(for velocityFormatter: VelocityFormatter) -> Double {
return dataSource.workoutInstantVelocity(for: self)
}
}

UICollectionViewCell.swift

    import UIKit

final class MeasurementCollectionViewCell: UICollectionViewCell {

@IBOutlet private var measurementPropertyLabel: UILabel!
@IBOutlet private var measurementValueLabel: UILabel!
@IBOutlet private var measurementUnitLabel: UILabel!

static let preferredReuseIdentifier = "Measurement Cell"
static let preferredNib = UINib(nibName: "MeasurementCollectionViewCell", bundle: nil)

override func awakeFromNib() {
super.awakeFromNib()
updateMeasurement(property: "Speed", value: "100", unit: "km/h")

//measurementValueLabel.font = measurementValueLabel.font.monospacedDigitFont
}

func updateMeasurement(property: String, value: String, unit: String?) {
measurementPropertyLabel.text = property
measurementValueLabel.text = value
measurementUnitLabel.text = unit
}

}

最佳答案

UICollectionView delegate 方法 collectionView(_, didSelectItemAt _) 中获取单元格的实例。

extension WorkoutDataViewControllerCV: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if let cell = collectionView.cellForItem(at: indexPath) as? MeasurementCollectionViewCell {
cell.selectedIndexPath(indexPath)
}
}
}

indexPath 将作为方法 selectedIndexPath 中的参数从上述方法传递到 MeasurementCollectionViewCell

class MeasurementCollectionViewCell: UICollectionViewCell {

......
func selectedIndexPath(_ indexPath: IndexPath) {

//Do your business here.
}
}

关于ios - 从 UICollectionViewController 获取 indexPath 到 UICollectionViewCell 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193812/

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