Sie sind auf Seite 1von 2

// SAP Fiori for iOS Mentor

// SAP Cloud Platform SDK for iOS Code Example


// Collection View
// Copyright 2017 SAP SE or an SAP affiliate company. All rights reserved.

import SAPFiori

struct Product {
var name = ""
var imageUrl = ""
}

class FioriCollectionViewController: UIViewController {

// prepare the products to be shown


let products = [
Product(name: "Notebook Basic 15", imageUrl: ""), // TODO: Maintain
your imageUrl
Product(name: "Notebook Basic 17", imageUrl: ""), // TODO: Maintain
your imageUrl
Product(name: "Notebook Professional 15", imageUrl: "") // TODO:
Maintain your imageUrl
// more data ...
]

@IBOutlet weak var collectionView: UICollectionView! // Refer to the


CollectionView added in your storyboard

override func viewDidLoad() {


super.viewDidLoad()
// Register Fiori collection view cell for showing a product information
collectionView.register(FUISimpleCollectionViewCell.self,
forCellWithReuseIdentifier:
FUISimpleCollectionViewCell.reuseIdentifier)
collectionView.register(FUICollectionSectionHeaderFooterView.self,
forSupplementaryViewOfKind:
UICollectionElementKindSectionHeader,
withReuseIdentifier: "Header")

collectionView.dataSource = self
collectionView.delegate = self

setFioriCollectionViewLayout()
}

// initialize FioriCollectionView and add it to the view


func setFioriCollectionViewLayout () {
// Set item layout in the collection view
let layout = FUICollectionViewLayout.horizontalFlow
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.headerReferenceSize = CGSize(width: 300, height: 44)
layout.minimumScaledItemSize = CGSize(width: 180.0, height: 174.0)
collectionView.setCollectionViewLayout(layout, animated: true)
}
}

extension FioriCollectionViewController: UICollectionViewDataSource,


UICollectionViewDelegate {
func numberOfSections(in: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView,


numberOfItemsInSection section: Int) -> Int {
return products.count
}

func collectionView(_ collectionView: UICollectionView,


viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryView(
ofKind: UICollectionElementKindSectionHeader,
withReuseIdentifier: "Header", for: indexPath)
if let view = view as? FUICollectionSectionHeaderFooterView {
view.style = .attribute
view.titleLabel.text = "Products"
view.attributeLabel.text = "See All"
view.didSelectHandler = {
print("See All tapped")
}
}
return view
}

func collectionView(_ collectionView: UICollectionView,


cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{

let fioriCVCell = collectionView.dequeueReusableCell(


withReuseIdentifier:
FUISimpleCollectionViewCell.reuseIdentifier,
for: indexPath) as! FUISimpleCollectionViewCell

fioriCVCell.contentImage = UIImage(named:
products[indexPath.item].imageUrl)
fioriCVCell.titleText = products[indexPath.item].name
return fioriCVCell

Das könnte Ihnen auch gefallen