Swift - выпуск доступа к данным из UICollectionView, встроенного в UITableView - PullRequest
0 голосов
/ 09 апреля 2020

У меня есть tableView с ячейкой-прототипом, содержащей UICollectionView. Я настроил tableView в соответствии с этим руководством (https://medium.com/@stasost / ios, как построить представление таблицы с несколькими типами ячеек 2df91a206429 ), и Пользовательский интерфейс работает. Я могу передать данные через мой tableView и в collectionView.

Макет вида

Когда выбран collectionViewCell, он переходит к другому виду.

Я не выяснил, как получить доступ к данным из collectionViewCell и передать их в новое представление.

CollectionView инициализируется в ячейке прототипа tableView. Я пробовал didSelectRow -> prepareForSegue (код ниже), но команды не выполняют автозаполнение и не работают.

Вот код для tableViewCell, где настраивается collectionView.


РЕДАКТИРОВАТЬ: Удален закомментированный код для ясности

import UIKit

class homeFeedTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var feedCollectionView: UICollectionView!
    var selectedEvent : Event? 

    var collectionItems = [CollectionViewModelItem]()
    var collectionItem : CollectionViewModelItem?

    @IBOutlet weak var sectionHeadingLabel: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        feedCollectionView.delegate = self
        feedCollectionView.dataSource = self

        print("collection items \(collectionItems.count)")
        for item in collectionItems{print("type: \(item.type), title: \(item.eventTitle)")}

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    //    setup view model
      var item: TableViewModelItem? {
                didSet {
    //                if not right class, skip
                    guard let item = item as? TableViewModelFeed else {
                        return
                    }
                    sectionHeadingLabel.text = item.sectionTitle
                }
            }

    //    create reuse identifier property
            static var identifier: String {
                return String(describing: self)
            }



}


import Foundation
import UIKit

extension homeFeedTableViewCell {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//    print("dataCount3: \(collectionItems.count) \(collectionItems[collectionItems.count-1].type)")
    return collectionItems.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//    let cell = UICollectionViewCell()
//    return cell

    self.collectionItem = collectionItems[indexPath.row]

    switch collectionItem!.type {
    case .yourEvents:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier:YourEventsCollectionViewCell.identifier, for: indexPath) as? YourEventsCollectionViewCell{

            cell.item = collectionItem
            print(cell.item?.type)
            print(".yourEvents")
            return cell
        }
    case .feed:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: mainFeedCollectionViewCell.identifier, for: indexPath) as? mainFeedCollectionViewCell{
            cell.item = collectionItem
            print(".feed")

            return cell
        }
    }
    return UICollectionViewCell()

}

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("\(collectionItems[indexPath.row].eventTitle) tapped")

        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "yourEventsToEventViewController" || segue.identifier == "feedToEventViewController"{
                print("prepare for segue1")
                let destinationVC = segue.destination as! EventViewController
                if collectionItem != nil{
                    print("prepare for segue2")
                    destinationVC.backgroundImageUrl = collectionItem!.backgroundImageUrl
                }
            }
        }


    }

}

1 Ответ

0 голосов
/ 09 апреля 2020

UICollectionView отслеживает свои выбранные indexPaths со свойством indexPathsForSelectedItems . Поскольку вы запускаете переход в collectionView(didSelectItem: atIndexPath:), выбранный вами indexPath доступен в течение prepare(forSegue:). Вы можете попробовать следующее:

class MyViewController: UIViewController, UICollectionViewDelegate {

    ...

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        performSegue(withIdentifier: "mySegue", sender: self)
    }

    ...

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)

        guard let destinationVC = segue.destination as! EventViewController,
            segue.identifier == "mySegue" else { return }

        // In this context, your selected cell is the one who fired the segue

        if let selectedIndexPaths = collectionView.indexPathsForSelectedItems,
            let firstSelectedIndexPath = selectedIndexPaths.first {

            let selectedObject = collectionItems[firstSelectedIndexPath.row]

            destinationVC?.backgroundUrl = selectedObject.backgroundUrl
        }
    }
}

Последовательность:

  1. Вы выбираете ячейку (через взаимодействие с пользователем, ie нажатие).
  2. didSelect выполняет переход с именем «mySegue» (в этом примере).
  3. В prepareForSegue вы ищете выбранные пути индекса. Предполагая, что вы не используете множественный выбор, вам нужен ваш первый и единственный indexPath. Используя этот путь индекса, вы можете получить данные в массиве collectionItems.
...