В моем ViewController
есть collectionView
с двумя ячейками, в одной находится карта, а во второй описание маршрута(таймер, пройденное расстояние и средний темп передвижения), а так же кнопка pauseButton
(во ViewController
). Cell
имеют размер экрана, т.е. они скролятся, и на экране видно только одну ячейку.
По нажатию на кнопку pauseButtonAction
я хочу, чтобы в обоих ячейках выполнялось locationManager.stopUpdatingLocation()
. Он выполняется только в той ячейке, которая видна на экране, вторую он не видит.
tableView.cellForItem(indexPath)
не помогает, tableView.visibleCells
так же очевидно не работает.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let runCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RunCollectionViewCell", for: indexPath) as! RunCollectionViewCell
let mapCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MapCollectionViewCell", for: indexPath) as! MapCollectionViewCell
if indexPath.item == 0 && indexPath.section == 0 {
return mapCell
} else {
return runCell
}
///////////////
@IBAction func pauseButtonAction(_ sender: Any) {
let indexPathRunCell = IndexPath(item: 1, section: 0)
let indexPathMapCell = IndexPath(item: 0, section: 0)
if let mapCell = collectionView.cellForItem(at: indexPathMapCell) as? MapCollectionViewCell {
print("MapCell найдена")
mapCell.locationManager.stopUpdatingLocation()
} else {
print ("mapCell не найдена")
}
if let runCell = collectionView.cellForItem(at: indexPathRunCell) as? RunCollectionViewCell {
print("runCell найдена")
runCell.locationManager.stopUpdatingLocation()
runCell.didTapPauseButton()
} else {
print("runCell не найдена")
}