更改 UICollectionView 数据源
Changing UICollectionView DataSource
我想在用户点击 UISegment
control
时更改 CollectionView DataSource
但不知道如何实现。这是代码的一部分:
查看控制器单元格:
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
}
}
视图控制器数据结构:
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")!)
]
}
}
视图控制器:
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 数据源,委托:
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
}
}
UICollectionView
对象具有 dataSource 属性,因此您可以轻松更改它。
请记住,数据源必须采用UICollectionViewDataSource
协议。
您可以使用开关来检测选定的段索引,然后您可以根据需要分配数据源。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//use switch here and call one of your methods (showWebInfo(),showProjectInfo() etc) like projects = showProjectInfo()
return projects.count
}
您必须在以下时间做同样的事情:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.Cellidentifier, forIndexPath: indexPath) as! ProjectsCollectionViewCell
//use switch here and call one of your methods (showWebInfo(),showProjectInfo() etc) like projects = showProjectInfo()
cell.project = self.projects[indexPath.item]
return cell
}
您没有调用 reload CollectionView 从 dataSource 加载数据。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController!.navigationBar.topItem!.title = "";
collectionView.reloadData()
}
这是一个答案
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var returnValue = 0
switch (mySegmentControl.selectedSegmentIndex) {
case 0:
returnValue = projects.count
case 1:
returnValue = web.count
case 2:
returnValue = deym.count
default:
break
}
return returnValue
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.Cellidentifier, forIndexPath: indexPath) as! ProjectsCollectionViewCell
switch (mySegmentControl.selectedSegmentIndex) {
case 0:
cell.project = self.projects[indexPath.item]
case 1:
cell.project = self.web[indexPath.item]
case 2:
cell.project = self.deym[indexPath.item]
default:
break
}
return cell
}
}
我想在用户点击 UISegment
control
时更改 CollectionView DataSource
但不知道如何实现。这是代码的一部分:
查看控制器单元格:
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
}
}
视图控制器数据结构:
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")!)
]
}
}
视图控制器:
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 数据源,委托:
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
}
}
UICollectionView
对象具有 dataSource 属性,因此您可以轻松更改它。
请记住,数据源必须采用UICollectionViewDataSource
协议。
您可以使用开关来检测选定的段索引,然后您可以根据需要分配数据源。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//use switch here and call one of your methods (showWebInfo(),showProjectInfo() etc) like projects = showProjectInfo()
return projects.count
}
您必须在以下时间做同样的事情:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.Cellidentifier, forIndexPath: indexPath) as! ProjectsCollectionViewCell
//use switch here and call one of your methods (showWebInfo(),showProjectInfo() etc) like projects = showProjectInfo()
cell.project = self.projects[indexPath.item]
return cell
}
您没有调用 reload CollectionView 从 dataSource 加载数据。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController!.navigationBar.topItem!.title = "";
collectionView.reloadData()
}
这是一个答案
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var returnValue = 0
switch (mySegmentControl.selectedSegmentIndex) {
case 0:
returnValue = projects.count
case 1:
returnValue = web.count
case 2:
returnValue = deym.count
default:
break
}
return returnValue
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.Cellidentifier, forIndexPath: indexPath) as! ProjectsCollectionViewCell
switch (mySegmentControl.selectedSegmentIndex) {
case 0:
cell.project = self.projects[indexPath.item]
case 1:
cell.project = self.web[indexPath.item]
case 2:
cell.project = self.deym[indexPath.item]
default:
break
}
return cell
}
}