Created
February 16, 2022 04:18
-
-
Save TachibanaKaoru/a6dea8f1a221ba9d68e3098b0df2b10e to your computer and use it in GitHub Desktop.
UICollectionViewListCell defaultContentConfiguration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct WeatherInfo{ | |
let title: String | |
let subTitle: String | |
let iconName: String | |
let description: String | |
} | |
class ViewController: UIViewController { | |
private var weatherCollectionView: UICollectionView? = nil | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let listConfiguration = UICollectionLayoutListConfiguration(appearance: .insetGrouped) | |
let simpleLayout = UICollectionViewCompositionalLayout.list(using: listConfiguration) | |
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: simpleLayout) | |
collectionView.register(UICollectionViewListCell.self, forCellWithReuseIdentifier: "cell") | |
collectionView.dataSource = self | |
view.addSubview(collectionView) | |
weatherCollectionView = collectionView | |
} | |
private var sampleData: [WeatherInfo]{ | |
let a = WeatherInfo( | |
title: "横浜", | |
subTitle: "神奈川", | |
iconName: "sun.max", | |
description: "きょうの関東地方は、冬型の気圧配置と強い寒気の影響で、平野部を中心に晴れるでしょう。北部の山沿いや山間部では雪が降る見込みです。") | |
let b = WeatherInfo( | |
title: "松本", | |
subTitle: "長野", | |
iconName: "cloud.rain", | |
description: "長野県は、曇りや晴れで、雪の降っている所があります。") | |
let c = WeatherInfo( | |
title: "仙台", | |
subTitle: "宮城", | |
iconName: "cloud", | |
description: "低気圧が日本海にあって、ゆっくり北東へ進んでいます。また、別の低気圧が三陸沖にあって北東へ進んでいます。") | |
let d = WeatherInfo( | |
title: "旭川", | |
subTitle: "北海道", | |
iconName: "snowflake", | |
description: "北海道付近は、発達した低気圧が日本海に停滞するでしょう。") | |
let e = WeatherInfo( | |
title: "高松", | |
subTitle: "香川", | |
iconName: "cloud.sun", | |
description: "香川県は、高気圧に覆われて晴れていますが、西部では寒気や湿った空気の影響で概ね曇りとなり、雨や雪の降っている所があります。") | |
return [a,b,c,d,e] | |
} | |
} | |
extension ViewController: UICollectionViewDataSource { | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return sampleData.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewListCell | |
let info = sampleData[indexPath.row] | |
var cellConfiguration = cell.defaultContentConfiguration() | |
cellConfiguration.text = info.title | |
cellConfiguration.secondaryText = info.subTitle | |
cellConfiguration.image = UIImage(systemName: info.iconName) | |
cell.contentConfiguration = cellConfiguration | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment