ios - 获取从 UICollectionViewController 到 UICollectionViewCell 子类的 indexPath

标签 ios swift uicollectionviewcell

在我的 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 _)中获取cell的实例。

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 到 UICollectionViewCell 子类的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193812/

相关文章:

swift - 具有数值类型通用类型约束的最佳自定义运算符

ios - 预览 UICollectionViewCell

ios - 如何在 ios 中维护登录 session ?

swift - Swift 嵌套函数中的 Inout 参数

ios - SpriteKit 滚动背景图像显示图像之间的线条

swift - Swift 应用程序配色方案实现的最佳实践

ios - 在 iOS 12 中,当使用具有约束的自调整大小单元格创建 UICollectionView 时,重新加载布局时高度错误,直到我滚动

ios - UICollectionViewCell - 内容不会与单元格的 contentView 一起动画

ios - 嵌套的 Collection View 单元格不会根据方向变化调整大小

ios - 身份验证服务错误中的完成处理程序