ios개발/프로젝트

<ios 프로젝트2> 2. UICollectionView의 사용, About Views, layouts

studying develop 2020. 3. 14. 03:20

1. uicollectionView vs uiCollectionReusableView 둘이 뭐가 다르지,,

 

uicollectionview도 리유저블 하게 사용할 수 있는거 아니였나..?

[https://stackoverflow.com/questions/44268014/what-is-the-difference-between-uicollectionviewcell-and-uicollectionreusableview] 아닌 뉘앙스로 말하는데 잘 모르겠다.

 

UICollectionViewCell 

[https://developer.apple.com/documentation/uikit/uicollectionview]

A single data item when that item is within the collection view’s visible bounds.

 

UICollectionReusableView

A view that defines the behavior for all cells and supplementary views presented by a collection view.

 

UICollectionView

An object that manages an ordered collection of data items and presents them using customizable layouts.

 


Collection Views and Layout Objects

[https://developer.apple.com/documentation/uikit/uicollectionview]

이미지 크기에 맞게 보여줄 수 있으면 좋겠다.

 

문제가 내가 인스펙터에서 수정한 값이 절대 그렇게 셀 크기가 설정되지 않는 문제 였는데, 콜렉션뷰의 인스펙터 창에서  use estimated cell size인가? 그걸 고쳤더니, 내가 설정한대로 되었다.

 

내 생각에는 저 예상값을 사용하도록 하면, 예상값이 우선적으로 반영되는거 같다. 셀 크기 정하는 메커니즘에 대해서 알아보자.

 

https://stackoverflow.com/questions/38028013/how-to-set-uicollectionviewcell-width-and-height-programmatically/38028106

 

 


estimatedItemSize

The estimated size of cells in the collection view.

 

Discussion

Providing an estimated cell size can improve the performance of the collection view when the cells adjust their size dynamically. Specifying an estimate value lets the collection view defer some of the calculations needed to determine the actual size of its content. Specifically, cells that are not onscreen are assumed to be the estimated height.

 

estimatedItemSize는 셀 크기를 동적으로 변화 시킬때 콜렉션 뷰의 성능을 향상 시킨다......??(defer 의미를 몰랐다, 지연시키다.) 예상 값을 정하면 콜렉션 뷰가 하는 실제 콘텐츠의 크기를 결정하기 위한 계산을 지연시킨다. 특히, 스크린에 보이지 않는 셀들은 예상 값을 가진다고 가정한다.

 

그럼 언제 계산되는거야...??

 

예상값이랑 실제값 계산 사이의 관계를 모르겠다. 누가 먼저고, 실제값이 계산되기는 하는건지, 언제 예상값이 사용되는건지??


 

itemSize

The default size to use for cells.

[https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout/1617711-itemsize]

 

Discussion

If the delegate does not implement the collectionView(_:layout:sizeForItemAt:) method, the flow layout uses the value in this property to set the size of each cell. This results in cells that all have the same size.

The default size value is (50.0, 50.0).

 

바로 아래에 설명하려는 함수인 collectionView( layout : sizeForItemAt)을 구현하지 않으면 itemSize가 초기값으로 사용되는거 같다.

 

 


collectionView(_:layout:sizeForItemAt:)

Asks the delegate for the size of the specified item’s cell.

[https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617708-collectionview]

 

 

Discussion

If you do not implement this method, the flow layout uses the values in its itemSize property to set the size of items instead. Your implementation of this method can return a fixed set of sizes or dynamically adjust the sizes based on the cell’s content.

 

이 메소드를 구현 안하면, itemSize로 셀 크기가 설정된다 한다.


음 어쨋든 위에 내용은 이론이고, 실제로 내가 원하는 바는 사진의 크기에 따라, 셀의 높이나 width도 조절되는 것이다. 

[https://baked-corn.tistory.com/124] 이동건님이 실제로 구현을 했다.

 

음 근데 이분은 내가 위에 말한, 고정값이랑 예상값 말고 auto contraint로 하셨다. 나는 그것에 실패했는데, 콜렉션 뷰에서 estimated 사이즈를 사용한다는 옵션을 끄고 하면 어떻게 될지 모르겠다. 다시해보자.

 

 


UICollectionViewFlowLayout

그리드 혹은 라인기반(lined-based) 레이아웃을 구현하는 데 사용되는 UICollectionViewFlowLayout에 대해 알아봅니다.

 

 

[https://www.edwith.org/boostcourse-ios/lecture/16912/]

 

이런 글이 있었다. 완전히 이해가 안되는데 보니까, 플로우 레이아웃은, 콜렉션 뷰 내에서 어떻게 배치할 것인지에 대한거 같다.

 


UICollectionViewDataSource 

 

개인적으로 궁금한게 UICollectionViewDataSource이 프로토콜은 언제 호출되는지 이다. 델리게이트를 정했을때 바로 호출되서 일일이 넣어주는건지, 아니면 셀이 뷰에 나타나려할때 호출되는건지.