檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)


問題描述

檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)

所以我有一個水平 UICollectionView 圖像,它位於垂直 UICollectionView 內,當我從垂直單元格中選擇一個單元格時,我想檢測哪個單元格位於水平 UICollectionView 的中心,我試圖發送通知並調用完成這項工作的函數,但由於它重複使用同一個單元格而被多次調用,所以最後我沒有得到適當的 indexPath。

而且當我點擊圖像時,“didSelectItemAt” 水平collectionView被調用,有沒有辦法讓垂直collectionView被調用?謝謝


參考解法

方法 1:

Your best bet would be delegates via protocols

Create protocol for your collection view cells

protocol yourDelegate: class {

    func didSelectCell(WithIndecPath indexPath: IndexPath, InCollectionView collectionView: UICollectionView) ‑> Void

}

In your cells, create a function called setup which you can call at cellForRow. In your cells, create a touch recogniser for self. Disable cell selection for your collection views since these delegates will be called when the user touches given cell.

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
    }

}

In your viewController, create extension for this protocol and if you do everything right, when the user touches your cell, the cell will call you via this delegation.

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..
    }

}

since protocol is ": class", we can use weak for your delegate property so no memory leak will occur. I use this method for tableViews and collectionViews throughout my projects.

(by Vince MohamedTomo Norbert)

參考文件

  1. Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center (CC BY‑SA 2.5/3.0/4.0)

#uicollectionviewcell #iOS #XCode #swift #uicollectionview






相關問題

Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)

UICollectionViewCell裡面的UIWebView不調用Delegate方法 (UIWebView inside UICollectionViewCell does not call Delegate Method)

Bộ sưu tập xem vấn đề thay đổi kích thước iPhone 4 và iPhone 5 (Collection view resizing issue iPhone 4 vs iPhone 5)

集合視圖單元格未出現 (Collection View Cells not appearing)

UICollectionView 單元格大小 ContentView 問題 (UICollectionView cell size ContentView Issue)

在 Swift 中使用 CollectionView 進行網格佈局 (Grid layout with CollectionView in Swift)

如何為collectionviewcell的刪除自定義動畫? (How to custom animation for collectionviewcell's deletion?)

檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)

navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)

XCode UICollectionViewCell 在模擬器上看起來比在故事板中小得多 (XCode UICollectionViewCell appearing much smaller on simulator than in storyboard)

CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)

由 LPLinkView 和 UIImageView 組成的單元格的 CollectionView 速度很慢,並且在滾動時會重新加載數據 (CollectionView with Cells made up of LPLinkView's and UIImageView's is Slow and Reloads Data While Scrolling)







留言討論