Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save helloworldsmart/52acb5c037d5c971486b0ddb0c49e4af to your computer and use it in GitHub Desktop.

Select an option

Save helloworldsmart/52acb5c037d5c971486b0ddb0c49e4af to your computer and use it in GitHub Desktop.
// 必須實作的方法:每個 cell 要顯示的內容
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// 依據前面註冊設置的識別名稱 "Cell" 取得目前使用的 cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
// 第一個在陣列取0
guard indexPath.item != 0 else {
cell.backgroundColor = .brown
return cell
}
// 設置 cell 內容 (即自定義元件裡 增加的圖片與文字元件)
cell.imageView.image = UIImage(named: "0\(indexPath.item + 1).jpg")
cell.titleLabel.text = "0\(indexPath.item + 1)"
return cell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment