【问题标题】:Instead of deleting an array I want it to send the array to another UIViewController而不是删除一个数组,我希望它将数组发送到另一个 UIViewController
【发布时间】:2020-12-15 22:51:19
【问题描述】:

我正在尝试创建一个函数,允许用户选择多个视频,然后选择一个按钮,它将所选视频发送到另一个数组。我已经有一个类似的功能来处理删除。我真的只是想重新利用我已经拥有的用于删除的代码,但我尝试过的一切都失败了。我是 Swift 新手,但有没有办法做到这一点或我应该采取更好的方法?

var videos = [PHAsset]()
var dictionarySelectedIndexPath: [IndexPath: Bool] = [:]

@objc func didDeleteButtonClicked(_ sender: UIBarButtonItem) {
    var deleteNeededIndexPaths: [IndexPath] = []
    for (key, value) in dictionarySelectedIndexPath {
        if value {
            deleteNeededIndexPaths.append(key)
        }
    }
    for i in deleteNeededIndexPaths.sorted(by: { $0.item > $1.item }) {
        videos.remove(at: i.item)
    }
    collectionView.deleteItems(at: deleteNeededIndexPaths)
    dictionarySelectedIndexPath.removeAll()
}

func getVideos() {
    let assets = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: nil)
    assets.enumerateObjects({ (object, count, stop) in
        self.videos.append(object)
    })
    
    self.videos.reverse()
    self.collectionView.reloadData()
    collectionView.delegate = self
    collectionView.dataSource = self
    let nib = UINib(nibName: "ItemCollectionViewCell", bundle: nil)
    collectionView.register(nib, forCellWithReuseIdentifier: cellIdentifier)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "videoEditorSegueIdentifier" {
        let otherVc = segue.destination as! VideoEditorVC
        otherVc.videos = videos
    }
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ItemCollectionViewCell            
        let asset = videos[indexPath.row]
        let manager = PHImageManager.default()

        if cell.tag != 0 {manager.cancelImageRequest(PHImageRequestID(cell.tag))}
        
        cell.tag = Int(manager.requestImage(for: asset, targetSize: CGSize(width: 120.0, height: 120.0), contentMode: .aspectFill, options: nil) { (result, _) in cell.imageView?.image = result
        })
        
        return cell
    }

【问题讨论】:

    标签: ios swift uiviewcontroller


    【解决方案1】:

    据我所知,您遇到的问题是因为您将 indexPath 存储在字典中以记住选择的内容,并且您难以将其转换为您持有的实际数据。

    如果您去掉中间人,并在 didSelectItemAt 方法中简单地用实际选定的对象填充数组,这会容易得多。

    以下内容:

    var selectedVideos = [PHAsset]()
    
    func videoFor(indexPath: IndexPath) -> PHAsset {
        // return the video more or less the same as you do it in cellForItemAt:
    }
    
    func indexFor(video: PHAsset) -> Int? {
        return selectedVideos.firstIndex(of: video)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let video = videoFor(indexPath: IndexPath)
        if selectedVideos.contains(video) {
            selectedVideos.remove(at: index)
        } else {
            selectedVideos.append(video)
        }
    }
    

    【讨论】:

    • 我添加了代码,但在 let video = videoFor 行我收到一条错误消息:“无法将 'IndexPath.type' 类型的值转换为预期的参数类型 'UICollectionView'”,并且在selectedVideos.remove 行我收到一条错误消息,提示“无法将类型(Any)-> Int 的值转换为预期的参数类型'Int'。我还用 cellForItemAt 更新了我的 OP,这样你就可以看到我在那里做了什么。我怀疑这是问题的根源,但我可以说出来。
    • 我上面写的代码更像是一个指南,而不是你可以使用的实际代码。你的错误已经够冗长了,我建议你花点时间自己解决它们:)
    猜你喜欢
    • 2017-02-16
    • 2019-12-30
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多