공부 기록
TableView의 Cell에서 contentConfiguration 프로퍼티가 있듯이 CollectionView의 Cell에서도 contentConfiguration이 있다. 하지만 UITableViewCell에는 defaultContentConfiguration()이 있지만, UICollectionView에서는 해당 프로퍼티를 가지고 있지 않다.
[ iOS ] contentConfiguration: TableView에 기본 Cell 사용하기 (iOS 14+)
공부 기록 UITableViewCell은 커스텀하지 않고도 cell을 구성할 수 있도록 여러 옵션을 제공한다. 가장 간단한 방법으로는 UITableViewCell의 프로퍼티 중 textLabel, detailTextLabel, imageView를 사용하는 것이였
88yhtserof.tistory.com
해당 프로퍼티가 없지만 ContentConfiguration을 지정해주는 방법은 UITableViewCell과 다르지 않다. UITableViewCell.defaultContetnConfiguration()의 반환되는 타입을 보면 바로 UIListContentConfiguration이다.
또 UIListContentConfiguration가 정의가 부분을 보면, UIContentConfigurtion 프로토콜을 준수하고 있는 것을 알 수 있다. 즉, UICollectionViewCell에는 defaultContentConfiguration()은 없지만 UILisrContentConfiguration 객체를 생성해 동일한 기능을 구현할 수 있다.
여기서 UIListContentConfiguration란 목록 기반의 View에 대한 Content Configuration로, UITableView, UICollectionView 또는 UIListContentView로 만든 목록들의 헤더, 풋터, cell를 구성할 수 있다.
사용 예시
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StationDetailCollectionViewCell", for: indexPath) as? StationDetailCollectionViewCell else { return UICollectionViewCell() }
var content = UIListContentConfiguration.cell()
content.text = "\(indexPath.row)"
content.secondaryText = "\(indexPath.row)번 collectionViewCell"
content.image = .init(systemName: "bus")
content.imageProperties.tintColor = .systemBlue
cell.contentConfiguration = content
return cell
}
UIListContentConfiguration
UITableViewCell.defaultContentConfiguration() 사용예시
'iOS' 카테고리의 다른 글
[ iOS ] Property list (0) | 2023.01.20 |
---|---|
[ iOS ] 공유하기 기능 구현 - UIActivityViewController (0) | 2023.01.03 |
[ iOS ] contentConfiguration: TableView에 기본 Cell 사용하기 (iOS 14+) (0) | 2022.11.29 |
[ iOS ] UIButton.Configuration 사용하기(1) - Init, Title (iOS 15) (0) | 2022.10.13 |
[iOS ] 데이터 전달 : (3/5) NotificationCenter로 전달하기 (0) | 2022.06.21 |