Program Tip

CollectionView sizeForItemAtIndexPath가 호출되지 않았습니다.

programtip 2020. 10. 22. 22:19
반응형

CollectionView sizeForItemAtIndexPath가 호출되지 않았습니다.


이것은 나를 미치게 만든다! 아래와 같이 UICollectionViewController가 있습니다.

class PhrasesCompactCollectionViewController: UICollectionViewController

numberOfSections 및 cellForItemAt이 호출되지만 sizeForItemAtIndexPath는 호출되지 않습니다. 다른 곳에서 똑같은 코드를 사용하고 있으며 올바르게 실행됩니다. Xcode 8 Beta 6을 사용하고 있습니다.

func collectionView(collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAtIndexPath indexPath:  NSIndexPath) -> CGSize {

        return CGSize(width: 120, height:120) 
    }

UICollectionViewDelegateFlowLayout클래스 선언에서 프로토콜을 구현하도록 지정해야합니다 .

class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout


에서 스위프트 3+ 이 방법을 사용 :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width:(collectionView.frame.height-90)/2, height: 100)
}

수업이 Delegate를 모두 준수하는지 확인하십시오.

  • UICollectionViewDelegate

  • UICollectionViewDelegateFlowLayout


스위프트 3

UICollectionView의 Cell Size를 설정하려면 UICollectionViewFlowLayout. 속성을 사용자 지정하고 해당 레이아웃 개체를 UICollectionView 개체로 설정합니다.

요구 사항 (원하는 셀 높이 및 너비)에 따라 cellheight 및 cell cellWidth 개체를 변경합니다.

override func viewDidLoad() {
    super.viewDidLoad()

    let cellWidth : CGFloat = myCollectionView.frame.size.width / 4.0
    let cellheight : CGFloat = myCollectionView.frame.size.height - 2.0
    let cellSize = CGSize(width: cellWidth , height:cellheight)

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //.horizontal
    layout.itemSize = cellSize
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.minimumLineSpacing = 1.0
    layout.minimumInteritemSpacing = 1.0
    myCollectionView.setCollectionViewLayout(layout, animated: true)

    myCollectionView.reloadData()
}

확인 : sizeForItemAt indexPath 메서드를 추가 한 경우 메서드를 제거해야합니다.

아래 제거 방법

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

}

첫 번째 클래스는 UICollectionViewDelegateFlowLayout에 확인해야합니다. 그런 다음 viewDidLoad ()에 다음 코드를 작성해야합니다.

// UICollectionView에 UIViewController의 UICollectionViewDelegateFlowLayout 메서드를 사용하도록 지시합니다.

collectionView.delegate = self

// 컬렉션보기 흐름 레이아웃을 설정하려는 경우

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //depending upon direction of collection view

self.collectionView?.setCollectionViewLayout(layout, animated: true)

이 코드를 사용하여 UICollectionViewDelegateFlowLayout 메서드

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

이 메서드에서 크기를 설정할 수 있습니다.


어젯밤에이 문제가 발생했습니다. 마지막으로 자정에 'didSelectItemAtIndex'메서드가 닫힘 괄호 "}"로 닫히지 않았 음을 깨달았을 때 문제를 해결했습니다.

I added a closure bracket at the very bottom of the class when asked by the compile error. So in effect all the methods below 'didSelectItemAtIndex' were inside that method.

Posting here just in case anyone else wastes 4 hours of their evenings on this one :-(


Make sure you put this in viewDidLoad()

collectionView.delegate = self

I had the same problem and nothing would fix it. I had a yellow warning saying to make it private because it came close to another function defined by the Layout Delegate. What fixed it for me was:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

Notice the difference from "sizeForItemAtIndexPath" to the correct "sizeForItemAt".

I tore my hair out for days trying to figure this out and it finally worked. Below I will include all of my function to show you how I also "hid" a cell by making the height equal to 0.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let findIndex = labelArray.index(of: "\(labelArray[indexPath.row])")
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width

    if (findIndex! % 2 == 0){
        // Even index, show cell
        return CGSize(width: screenWidth, height: 246)
    }
    else {
        return CGSize(width: screenWidth, height: 0)
    }
}

For me (in Swift 3.1) the method must be declared public like this:

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       return CGSize()
    }

Don't forget to make it public.

참고URL : https://stackoverflow.com/questions/39107482/collectionview-sizeforitematindexpath-never-called

반응형