如何通过segue或委托将数据从TableViewCell传递到Swift 4或更高版本中的CollectionViewController?

我目前正在做的作业有问题。当我在名为GalleryChooserViewController的TableViewController中选择一行时,我试图设置一个属性以及编辑名为ImageGalleryViewController的CollectionViewController。我已成功传递数据,但在GalleryChooserViewController中选择了一行后,无法在ImageGalleryViewController中拖放图像。

以下是相应视图控制器中的一些代码段。

GalleryChooserViewController

weak var delegate : GalleryChooserDelegate?
override func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    chosenAlbum = albums[indexPath.row]
    delegate?.choose(gallery: chosenAlbum)
    if let detailViewController = delegate as? ImageGalleryViewController,let detailNavigationController = detailViewController.navigationController {
        splitViewController?.showDetailViewController(detailNavigationController,sender: nil)
    }

}

ImageGalleryViewController

func collectionView(_ collectionView: UICollectionView,performDropWith coordinator: UICollectionViewDropCoordinator) {
    let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0,section: 0)

    for item in coordinator.items {

        if let sourceIndexPath = item.sourceIndexPath {
            if let image = item.dragItem.localObject as? UIImage {
                collectionView.performBatchUpdates({
                    gallery.remove(at: sourceIndexPath.item)
                    gallery.insert(image,at: destinationIndexPath.item)
                    collectionView.deleteItems(at: [sourceIndexPath])
                    collectionView.insertItems(at: [destinationIndexPath])
                    storedImages.updateValue(gallery,forKey: chosenGallery)
                })
                coordinator.drop(item.dragItem,toItemAt: destinationIndexPath)
            }
        } else {

            let placeholderContext = coordinator.drop(item.dragItem,to:UICollectionViewDropPlaceholder(insertionIndexPath: destinationIndexPath,reuseIdentifier: "DropPlaceholderCell"))
            item.dragItem.itemProvider.loadObject(ofClass: NSURL.self){ (provider,error) in

                DispatchQueue.global(qos: .userInitiated).async {
                    if let url = provider as? URL {
                        let urlToBeFetched = url.imageURL
                        let urlContents = try? Data(contentsof: urlToBeFetched)
                        DispatchQueue.main.async {
                            if let imageData = urlContents,url.imageURL == urlToBeFetched {
                                self.image = UIImage(data: imageData)
                                placeholderContext.commitInsertion(dataSourceUpdates: { insertionIndexPath in
                                    self.gallery.insert(self.image!,at: insertionIndexPath.item)
                                    self.storedImages.updateValue(self.gallery,forKey: self.chosenGallery)

                                })
                            }
                        }
                    }
                    else {
                        placeholderContext.deletePlaceholder()
                    }
                }
            }
        }
    }
}
extension ImageGalleryViewController : GalleryChooserDelegate {
func choose(gallery: String) {
    chosenGallery = gallery
    if let gallery = storedImages[gallery] {
        self.gallery = gallery
     }
    else {
        storedImages.updateValue(self.gallery,forKey: gallery)
       }
}

}

SceneDelegate.swift

func scene(_ scene: UIScene,willConnectTo session: UIScenesession,options connectionOptions: UIScene.ConnectionOptions) {
    guard
        let splitViewController = window?.rootViewController as? UISplitViewController,let leftNavController = splitViewController.viewControllers.first as? UINavigationController,let masterViewController = leftNavController.viewControllers.first as? GalleryChooserViewController,let detailViewController = (splitViewController.viewControllers.last as? UINavigationController)?.topViewController as? ImageGalleryViewController
        else { fatalError() }

      let firstAlbum = masterViewController.albums.first
      detailViewController.chosenGallery = firstAlbum!
      masterViewController.delegate = detailViewController
      detailViewController.navigationItem.leftItemsSupplementBackButton = true
      detailViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
    }
q273225308 回答:如何通过segue或委托将数据从TableViewCell传递到Swift 4或更高版本中的CollectionViewController?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3063194.html

大家都在问