如何在一个视图中添加多个集合视图
How to add multiple collection view in a view
我正在尝试将 2 个集合视图合并到同一个视图中。我实现了所有内容并注意到在第二个集合视图中加载的数据与第一个集合视图相同。
这是我正在尝试的代码
class DashBoardMainSection1CollectionViewCellClass: UICollectionViewCell
{
@IBOutlet weak var DashBoardMainSection1CollectionViewImageListingViewOutlet : UIImageView!
@IBOutlet weak var DashBoardMainSection1CollectionViewLabelOutlet : UILabel!
}
class DashBoardMainSection2CollectionViewCellClass: UICollectionViewCell
{
@IBOutlet weak var DashBoardMainSection2CollectionViewImageListingViewOutlet : UIImageView!
@IBOutlet weak var DashBoardMainSection2CollectionViewLabelOutlet : UILabel!
}
private let reuseIdentifierDashBoardMainSection1Cell = "dashBoardMainSection1CollectionViewCellIdentifier"
private let reuseIdentifierDashBoardMainSection2Cell = "dashBoardMainSection2CollectionViewCellIdentifier"
class DashBoardViewControllerMain: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
@IBOutlet weak var DashBoardMainSection1CollectionViewOutlet : UICollectionView!
@IBOutlet weak var DashBoardMainSection2CollectionViewOutlet : UICollectionView!
override func viewDidLoad()
{
super.viewDidLoad()
DashBoardMainSection1CollectionViewOutlet.delegate = self;
DashBoardMainSection2CollectionViewOutlet.delegate = self;
DashBoardMainSection1CollectionViewOutlet.dataSource = self;
DashBoardMainSection2CollectionViewOutlet.dataSource = self;
// Do any additional setup after loading the view.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
var count:Int?
if (self.DashBoardMainSection1CollectionViewOutlet) != nil{
count = 10
}
//if (self.DashBoardMainSection2CollectionViewOutlet) != nil{
else{
count = 5
}
return count!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let collectionView = self.DashBoardMainSection1CollectionViewOutlet{
let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass
cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png")
cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST"
return cell
}
else{
let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass
cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png")
cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND"
return cell2
}
}
当我 运行 项目时,两个集合视图都显示 Apple 图片并且单元格计数为 10。
谁能帮我解决这个问题...
如果两个 UICollectionView
的数据相同,则表示您使用的是相同的 DataSource
。
为每个 UICollectionView
定义一个单独的 DataSource
并且您应该对委托执行相同的操作,除非您需要对两者执行相同的操作。
因为您的条件 if (self.DashBoardMainSection1CollectionViewOutlet) != nil
将始终 return 为真,并且 if
块的代码只会每次执行。
试试这个:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
var count = 0
if (collectionView == self.DashBoardMainSection1CollectionViewOutlet) {
count = 10
}
// if (collectionView == self.DashBoardMainSection2CollectionViewOutlet)
else {
count = 5
}
return count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == self.DashBoardMainSection1CollectionViewOutlet){
let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass
cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png")
cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST"
return cell
}
else{
let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass
cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png")
cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND"
return cell2
}
}
问题出在if语句中:
应该是这样的:
if collectionView == self.DashBoardMainSection1CollectionViewOutlet{
//code
}
使用这段代码,您检查参数传递的 collectionView
是否是第一个 collectionView
。
使用您的原始代码,您创建了名为 collectionView
的新常量,并将其分配给您的第一个 collectionView
。
请使用标签管理两个集合视图。在 viewDidLoad 方法上或从情节提要中为两个 collectionview 提供标签。现在根据tag做条件
示例:-
DashBoardMainSection1CollectionViewOutlet.tag = 1
DashBoardMainSection2CollectionViewOutlet.tag = 2
if collectionView.tag == 1 {
return 10
} else {
return 5
}
我正在尝试将 2 个集合视图合并到同一个视图中。我实现了所有内容并注意到在第二个集合视图中加载的数据与第一个集合视图相同。
这是我正在尝试的代码
class DashBoardMainSection1CollectionViewCellClass: UICollectionViewCell
{
@IBOutlet weak var DashBoardMainSection1CollectionViewImageListingViewOutlet : UIImageView!
@IBOutlet weak var DashBoardMainSection1CollectionViewLabelOutlet : UILabel!
}
class DashBoardMainSection2CollectionViewCellClass: UICollectionViewCell
{
@IBOutlet weak var DashBoardMainSection2CollectionViewImageListingViewOutlet : UIImageView!
@IBOutlet weak var DashBoardMainSection2CollectionViewLabelOutlet : UILabel!
}
private let reuseIdentifierDashBoardMainSection1Cell = "dashBoardMainSection1CollectionViewCellIdentifier"
private let reuseIdentifierDashBoardMainSection2Cell = "dashBoardMainSection2CollectionViewCellIdentifier"
class DashBoardViewControllerMain: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
@IBOutlet weak var DashBoardMainSection1CollectionViewOutlet : UICollectionView!
@IBOutlet weak var DashBoardMainSection2CollectionViewOutlet : UICollectionView!
override func viewDidLoad()
{
super.viewDidLoad()
DashBoardMainSection1CollectionViewOutlet.delegate = self;
DashBoardMainSection2CollectionViewOutlet.delegate = self;
DashBoardMainSection1CollectionViewOutlet.dataSource = self;
DashBoardMainSection2CollectionViewOutlet.dataSource = self;
// Do any additional setup after loading the view.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
var count:Int?
if (self.DashBoardMainSection1CollectionViewOutlet) != nil{
count = 10
}
//if (self.DashBoardMainSection2CollectionViewOutlet) != nil{
else{
count = 5
}
return count!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let collectionView = self.DashBoardMainSection1CollectionViewOutlet{
let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass
cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png")
cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST"
return cell
}
else{
let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass
cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png")
cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND"
return cell2
}
}
当我 运行 项目时,两个集合视图都显示 Apple 图片并且单元格计数为 10。
谁能帮我解决这个问题...
如果两个 UICollectionView
的数据相同,则表示您使用的是相同的 DataSource
。
为每个 UICollectionView
定义一个单独的 DataSource
并且您应该对委托执行相同的操作,除非您需要对两者执行相同的操作。
因为您的条件 if (self.DashBoardMainSection1CollectionViewOutlet) != nil
将始终 return 为真,并且 if
块的代码只会每次执行。
试试这个:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
var count = 0
if (collectionView == self.DashBoardMainSection1CollectionViewOutlet) {
count = 10
}
// if (collectionView == self.DashBoardMainSection2CollectionViewOutlet)
else {
count = 5
}
return count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == self.DashBoardMainSection1CollectionViewOutlet){
let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass
cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png")
cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST"
return cell
}
else{
let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass
cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png")
cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND"
return cell2
}
}
问题出在if语句中:
应该是这样的:
if collectionView == self.DashBoardMainSection1CollectionViewOutlet{
//code
}
使用这段代码,您检查参数传递的 collectionView
是否是第一个 collectionView
。
使用您的原始代码,您创建了名为 collectionView
的新常量,并将其分配给您的第一个 collectionView
。
请使用标签管理两个集合视图。在 viewDidLoad 方法上或从情节提要中为两个 collectionview 提供标签。现在根据tag做条件
示例:-
DashBoardMainSection1CollectionViewOutlet.tag = 1
DashBoardMainSection2CollectionViewOutlet.tag = 2
if collectionView.tag == 1 {
return 10
} else {
return 5
}