我有一个像[String:[GIAAppointment]]
这样的字典。我想从中获取数据并像字典的键一样显示到集合视图中,假设它要进入节标题,并且我还想获取与每个键相对应的值计数,以显示每个节的项目数。我尝试了不同的代码,但无法修复。
dict就像:
[ "today":[GIAAppointment], "last2days":[GIAAppointment]...]
这是页面:
import UIKit
private let reuseIdentifier = "Cell"
class VideoCollectionViewController: UICollectionViewController {
var videoCategories: [videoCategory] = videoLibrary.fetchPhotos()
struct Storyboard {
static let videoClassCell = "VideoClassCell"
static let sectionHeaderView = "SectionHeaderView"
static let leftAndRightPaddings: CGFloat = 2.0
static let numberofItemsPerRow: CGFloat = 3.0
}
var appointmentGroups :[String:[GIAAppointment]] = [:]
var header:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
GIAServerAPI.getDemoClasses { (dict) in
self.appointmentGroups = dict as! [String:[GIAAppointment]]
for (key, value) in self.appointmentGroups {
self.header.append(key)
}
}
collectionView?.reloadData()
let collectionViewWidth = collectionView?.frame.width
let itemWidth = (collectionViewWidth! - Storyboard.leftAndRightPaddings) / Storyboard.numberofItemsPerRow
let layout = collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// return appointmentGroups.count
return header.count
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.videoClassCell, for: indexPath) as! VideoClassCell
cell.imageName = "hello"
// Configure the cell
return cell
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let sectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Storyboard.sectionHeaderView, for: indexPath) as! SectionHeaderView
sectionHeaderView.categoryTitle = "SectionHeader"
return sectionHeaderView
}
}