Skip to content

Instantly share code, notes, and snippets.

@TachibanaKaoru
Created February 16, 2022 06:09
Show Gist options
  • Save TachibanaKaoru/68f770ee382b2f595f586280169bb904 to your computer and use it in GitHub Desktop.
Save TachibanaKaoru/68f770ee382b2f595f586280169bb904 to your computer and use it in GitHub Desktop.
UICellAccessory
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
// UICellAccessory
let customAccessory1 = UICellAccessory.CustomViewConfiguration(
customView: UIImageView(image: UIImage(systemName: "bandage")),
placement: .leading(displayed: .always))
let customAccessory2 = UICellAccessory.CustomViewConfiguration(
customView: UIImageView(image: UIImage(systemName: "tray")),
placement: .leading(displayed: .always))
let customAccessory3 = UICellAccessory.CustomViewConfiguration(
customView: UIImageView(image: UIImage(systemName: "bicycle")),
placement: .trailing(displayed: .always))
cell.accessories = [.customView(configuration: customAccessory1),
.customView(configuration: customAccessory2),
.customView(configuration: customAccessory3),
.checkmark()]
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment