Ios 控制两个不同的collectionview

Ios 控制两个不同的collectionview,ios,swift,xcode,button,uicollectionview,Ios,Swift,Xcode,Button,Uicollectionview,我目前正试图控制两个不同的集合视图。我在每个集合视图中都嵌入了按钮,我想让每个集合视图都包含一组数组中的值。这是到目前为止我的代码,我不知道我应该做什么来实现这一点: 导入UIKit 类MyButtonCell:UICollectionViewCell{ @IBVAR按钮无:UIButton! @IBVAR目标按钮:UIButton! 变量回调:(()->())? 重写初始化(帧:CGRect){ super.init(frame:frame) commonInit() } 必需初始化?(编码器

我目前正试图控制两个不同的集合视图。我在每个集合视图中都嵌入了按钮,我想让每个集合视图都包含一组数组中的值。这是到目前为止我的代码,我不知道我应该做什么来实现这一点:

导入UIKit
类MyButtonCell:UICollectionViewCell{
@IBVAR按钮无:UIButton!
@IBVAR目标按钮:UIButton!
变量回调:(()->())?
重写初始化(帧:CGRect){
super.init(frame:frame)
commonInit()
}
必需初始化?(编码器:NSCoder){
super.init(编码器:编码器)
commonInit()
}
func commonInit()->Void{
contentView.layer.borderWidth=1
contentView.layer.borderColor=UIColor.black.cgColor
}
@iAction func按钮已映射(\发送方:UIButton){
回调?()
}
}
类StevenViewController:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource{
let Buttontiles:[字符串]=[
"4", "6", "7", "8"
]
var targetButtonTiles:[字符串]=[
"", "", "", ""
]
当前变量:String=“”
@IBOutlet var collectionView:UICollectionView!
@IBOutlet var targetCollection:UICollectionView!
重写func viewDidLoad(){
super.viewDidLoad()
targetCollection.delegate=self
targetCollection.dataSource=self
collectionView.delegate=self
collectionView.dataSource=self
}
func numberOfSections(在collectionView:UICollectionView中)->Int{
返回1
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回按钮数
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“myCellID”,for:indexPath)作为!MyButtonCell
让targetCell=targetCollection.DequeueReuseAbleCell(带有ReuseIdentifier:“myCellID”,用于:indexPath)作为!MyButtonCell
//设置按钮标题(以及任何其他属性)
cell.buttonOne.setTitle(buttonTitles[indexPath.item],用于:[])
targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item],用于:[])
//设置单元格的回调闭包
cell.callback={
打印(“在\(indexPath)点击按钮”)
self.targetButtonTiles.append(self.ButtonTiles[indexPath.item])
打印(self.targetButtonTiles)
//按一下按钮,做你想做的事
}
返回目标电池
}
}
ButtonTiles和TargetButtonTiles是我希望每个CollectionView包含的两个数组

这就是它现在的样子-两个都显示相同的数组。我知道我可能应该为单元格使用不同的标识符ID,但一旦我这样做了,它就会给我一个错误,告诉我“无法将具有标识符目标的kind:UICollectionElementKindCell视图出列”


在UICollectionViewDelegate和UICollectionViewDataSource中

您可以使用此代码段来标识当前UICollectionView

 if collectionView == [YOUR_COLLECTIONVIEW_QUTLET] {
       
      
 }