Получить indexPath из UICollectionViewController в подкласс UICollectionViewCell - PullRequest
0 голосов
/ 07 ноября 2018

в моем контроллере представления Я загружаю пользовательский CollectionViewCell с подклассом. Исходя из положения указателя в ячейке, я хочу по-разному форматировать текстовые метки. То есть В первой строке есть только одна ячейка с большим текстом, а во второй - две ячейки с меньшим текстом.

Как я могу получить доступ к indexpath из моего UICollectionView в моем подклассе UICollectionViewCell? Я пробовал протокол делегата, но он всегда возвращает ноль.

Код ниже и большое спасибо!

Markus

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
       }

    }

Ответы [ 3 ]

0 голосов
/ 07 ноября 2018

Получить экземпляр ячейки в 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.
    }
}
0 голосов
/ 07 ноября 2018

Довольно прямым путем было бы хранить indexPath в подклассе UICollectionViewCell класса. Назначьте его при возвращении из cellForRow at: index path. Так что теперь подклассная ячейка collectionview имеет доступ к своему собственному каталогу

0 голосов
/ 07 ноября 2018

Вы можете использовать цепочку респондента, чтобы получить представление коллекции ячейки, с которой вы можете получить путь индекса. Просто добавьте эти расширения в новый файл с именем UICollectionViewCell+IndexPath.swift.

extension UIResponder {
    func next<T: UIResponder>(_ type: T.Type) -> T? {
        return next as? T ?? next?.next(type)
    }
}

extension UICollectionViewCell {
    var indexPath: IndexPath? {
        return next(UICollectionView.self)?.indexPath(for: self)
    }
}

Теперь внутри вашей клетки вы можете использовать self.indexPath

...