Ich möchte ändern CollectionView DataSource
, wenn Benutzer auf die control
tippen, aber habe keine Ahnung, wie dies zu implementieren. Hier ist ein Teil des Codes:Ändern UICollectionView DataSource
View-Controller-Cell:
class ProjectsCollectionViewCell: UICollectionViewCell {
//MARK: - Public API
var project : Projects! {
didSet {
updateUI()
}
}
private func updateUI() {
titleLabel?.text! = project.title
subTitleLabel?.text! = project.title
featuredImageView?.image! = project.featuredImage
}
//MARK: - Private
@IBOutlet weak var featuredImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subTitleLabel: UILabel!
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = 10.0
self.clipsToBounds = true
}
}
-View-Controller-Daten struct:
class Projects {
//MARK: - Public API
var title = ""
var subTitle = ""
var featuredImage: UIImage!
init(title: String, subTitle: String, featuredImage: UIImage!) {
self.title = title
self.subTitle = subTitle
self.featuredImage = featuredImage
}
//MARK: - Private
static func showProjectInfo() -> [Projects] {
return [
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "1")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "2")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "3")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "4")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "5")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "6")!)
]
}
static func showWebInfo() -> [Projects] {
return [
Projects(title: "DA", subTitle: "SPASIBO", featuredImage: UIImage(named: "1")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "2")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "3")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "4")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "5")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "6")!)
]
}
static func showDeymInfo() -> [Projects] {
return [
Projects(title: "DA", subTitle: "PODJALUISTA", featuredImage: UIImage(named: "1")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "2")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "3")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "4")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "5")!),
Projects(title: "DA", subTitle: "PRIVET", featuredImage: UIImage(named: "6")!)
]
}
}
-View-Controller:
class ProjectsVC: UIViewController {
@IBOutlet weak var headerAlpha: UIImageView!
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var collectionView: UICollectionView!
//MARK: - UICollectionView DataSourse
private var projects = Projects.showProjectInfo()
private var web = Projects.showWebInfo()
private var deym = Projects.showDeymInfo()
override func viewDidLoad() {
super.viewDidLoad()
self.headerAlpha.backgroundColor = UIColor(red: 21/255, green: 55/255, blue: 80/255, alpha: 0.95)
}
//MARK: - CollectionView Data Sourse
private struct Storyboard {
static let Cellidentifier = "Project Cell"
}
@IBAction func showComponent(sender: UISegmentedControl) {
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController!.navigationBar.topItem!.title = "";
}
}
UICollectionView Datenquelle, Delegierter:
extension ProjectsVC: UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return projects.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.Cellidentifier, forIndexPath: indexPath) as! ProjectsCollectionViewCell
cell.project = self.projects[indexPath.item]
return cell
}
}
Sie nur Daten von 'projects' und reload von Collection –