UICollectionView 可用來建立網格、卡片或自訂排版。使用方式與 UITableView 類似,需要設定 layout、dataSource 與 delegate。
可繼承 UICollectionViewController,也可在一般 UIViewController 中建立 UICollectionView。後者需自行加入畫面並設定 delegate。
先建立 UICollectionViewFlowLayout,再用 initWithFrame:collectionViewLayout: 建立 collection view,註冊 cell,設定背景色、delegate 與 dataSource。
numberOfItemsInSection 回傳每節項目數;cellForItemAtIndexPath 回傳 cell;didSelectItemAtIndexPath 處理點擊。FlowLayout 的 delegate 可設定項目大小與 section 間距。