Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检测位于另一个uicollectionView内部的uicollectionView的哪个UICollectionViewCell位于中心_Ios_Swift_Xcode_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 检测位于另一个uicollectionView内部的uicollectionView的哪个UICollectionViewCell位于中心

Ios 检测位于另一个uicollectionView内部的uicollectionView的哪个UICollectionViewCell位于中心,ios,swift,xcode,uicollectionview,uicollectionviewcell,Ios,Swift,Xcode,Uicollectionview,Uicollectionviewcell,因此,我有一个水平的UICollectionView图像,它位于垂直的UICollectionView中,我想在我从垂直的单元格中选择一个单元格时,检测哪个单元格位于水平的UICollectionView的中心 我试图发送一个通知并调用一个函数来完成这项工作,但是由于它重复使用了同一个单元格,所以调用了多次,所以最后我没有得到适当的indexPath 还有,当我点击图像时,水平集合视图的“didSelectItemAt”被调用,有没有办法让垂直视图被调用? 谢谢您的最佳选择是通过协议的代表 为集

因此,我有一个水平的UICollectionView图像,它位于垂直的UICollectionView中,我想在我从垂直的单元格中选择一个单元格时,检测哪个单元格位于水平的UICollectionView的中心 我试图发送一个通知并调用一个函数来完成这项工作,但是由于它重复使用了同一个单元格,所以调用了多次,所以最后我没有得到适当的indexPath

还有,当我点击图像时,水平集合视图的“didSelectItemAt”被调用,有没有办法让垂直视图被调用?
谢谢

您的最佳选择是通过协议的代表

为集合视图单元格创建协议

protocol yourDelegate: class {
    
    func didSelectCell(WithIndecPath indexPath: IndexPath, InCollectionView collectionView: UICollectionView) -> Void
    
}
在单元格中,创建一个名为setup的函数,您可以在cellForRow调用该函数。 在您的单元格中,为自己创建一个触摸识别器。 禁用集合视图的单元格选择,因为当用户触摸给定单元格时将调用这些代理

class yourCell: UICollectionViewCell  {
    
    var indexPath: IndexPath? = nil
    var collectionView: UICollectionView? = nil
    
    weak var delegate: yourDelegate? = nil
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let selfTGR = UITapGestureRecognizer(target: self, action: #selector(self.didTouchSelf))
        self.contentView.addGestureRecognizer(selfTGR)
    }
    
    @objc func didTouchSelf() {
        guard let collectionView = self.collectionView, let indexPath = self.indexPath else {
            return
        }
        
        delegate?.didSelectCell(WithIndecPath: indexPath, InCollectionView: collectionView)
    }
    
    func setupCell(WithIndexPath indexPath: IndexPath, CollectionView collectionView: UICollectionView, Delegate delegate: yourDelegate) {
        self.indexPath = indexPath
        self.collectionView = collectionView
        self.delegate = delegate
    }
    
}
在viewController中,为该协议创建扩展,如果您一切正常,当用户触摸您的单元格时,该单元格将通过此委派呼叫您

extension YourViewController: yourDelegate {
    
    func didSelectCell(WithIndecPath indexPath: IndexPath, InCollectionView collectionView: UICollectionView) {
        //You have your index path and you can "if" your colllection view
        
        if collectionView == self.yourFirstCollectionView {
            
        } else if collectionView == self.yourSecondCollectionView {
            
        }
        //and so on..
    }
    
}
由于协议是“:class”,我们可以使用弱代理属性,这样就不会发生内存泄漏。在我的整个项目中,我使用这种方法处理TableView和CollectionView。

@VinceMohamed我很高兴:)如果答案对您有帮助,请接受