通过CollectionViewCell进行Swift委托

时间:2019-11-20 19:50:51

标签: ios swift uicollectionviewcell

我有个大UICollectionViewCells,里面有另一个(小)UICollectionViewController,所以基本上每个单元都有多个单元格。

问题是,如何通知/更新我的主要UICollectionViewController(具有较大的单元格),我的小UICollectionViewControllers中有变化

基本上,我在大型cellForItemAt的{​​{1}}中配置单元格,它的单元格具有UICollectionViewController的属性,在那里我配置了小型didSet

所以基本上,这是Trello-ish应用程序。 这是我的ListsViewController(有列表)

UICollectionViewController

并在我的override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ListCollectionViewCell.reuseId, for: indexPath) as! ListCollectionViewCell let list = lists[indexPath.row] cell.listName.text = " \(list.name)" cell.listId = list.id cell.delegate = self cell.cards = cards[list.id] cell.cardsController.collectionView.reloadData() return cell } 内:

ListCollectionViewCell

然后,在CardsCollectionViewController内部,我有一个方法lazy var cardsController = CardsCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout()) var delegate: CardsCollectionViewControllerDelegate? var cards: [String]? = [] { didSet { addSubview(cardsController.view) // constraints here cardsController.cards = cards ?? [] cardsController.listId = listId cardsController.delegate = delegate } } ,其中最后一张卡片的作用类似于按钮didSelectItemAt

"Add new Card"

所有API Network东西都可以正常工作,并且可以将卡添加到trello本身,现在最后一部分是:当我在警报上单击“确定”时,我需要调用override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let alert = UIAlertController(title: "Add card", message: "", preferredStyle: .alert) alert.addTextField() alert.addAction(UIAlertAction(title: "Back", style: .cancel, handler: { [] (_) in self.dismiss(animated: true, completion: nil) })) alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { [weak alert] (_) in let textField = alert?.textFields![0] let cardName = textField?.text NetworkManager.shared.createCardWith(name: cardName ?? "", onList: self.listId, completion: { self.delegate?.didAddCard() }) })) } } protocol CardsCollectionViewControllerDelegate { func didAddCard() } extension ListsViewController: CardsCollectionViewControllerDelegate { func didAddCard() { self.getDataFromTrello() } } 方法,该方法是getDataFromTrello()(第一),我不确定该怎么做。

0 个答案:

没有答案