开发者社区> 问答> 正文

如何在同一个视图控制器中将数据从一个集合视图传递到第二个集合视图

我有两个CollectionView在.ViewController,如何更改CollectionView_2_items项数据时,立即选择CollectionView_1_types牢房?

我的结构是:

struct MyObj {
    var type: String
    var items:[String]
}

let a = Myobj(type: "food", items:["apple", "ham", "egg"])
let b = Myobj(type: "color", items:["white", "black", "yellow"])
var Allobj:[Myobj] = [a, b]```  
我想展示并更改Myobj.项目CollectionView_2_item当我改变Myobj.type在……里面CollectionView_1_types.

以下是我在ViewController中的代码:

class MainControllerView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var TypeCollectionView: UICollectionView!
@IBOutlet weak var itemCollectionView: UICollectionView!

struct MyObj {
    var item: String = ""
    var Allitems: [String] = []
}

var Allobj:[Myobj] = []
var ary:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    self.TypeCollectionView.delegate = self
    self.TypeCollectionView.dataSource = self
    self.itemCollectionView.delegate = self
    self.itemCollectionView.dataSource = self

    let a = MyItem(item: "food", Allitems: ["apple", "ham", "egg"])
    let b = MyItem(item: "color", Allitems: ["white", "black", "yellow"])
    Allobj = [a, b]
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == TypeCollectionView {
        return Allobj.count
    }else if collectionView == itemCollectionView {
        return ary.count
    }
    return 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "", for: indexPath) as UICollectionViewCell

    if collectionView == TypeCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! TypeCollectionCell
        let d = Allobj[indexPath.row]
        cell.typeLabel.text = d.item
        return cell
    }else if collectionView == itemCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! itemsCollectionCell
        let d = ary[indexPath.row]
        cell.itemLabel.text = d
        return cell
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let d  = Allobj[indexPath.row]
    if collectionView == TypeCollectionView {
        ary = d.Allitems
    }
}

}

展开
收起
游客5akardh5cojhg 2019-12-04 14:22:52 482 0
1 条回答
写回答
取消 提交回答
  • 如果您已经以标准的方式实现了集合视图,这应该是非常简单的。

    将呈现视图控制器与UICollectionViewDelegate相一致,将收藏品视图1的委托设置为视图控制器,并实现collectionView(_:didSelectItemAt:)委托方法在这个委托方法中,加载您喜欢的任何数据。

    extension myViewController: UICollectionViewDelegate {
       func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
           getCV2Data(using: indexpath)() //use the collectionView's indexPath properties to prepare whatever data you need for collectionView2
           collectionView2.reloadData
       }
    }
    
    

    哪里getCV2Datat(using:)是一种方法,它设置必要的数据(如果需要的话),以便当‘corctionView 2’重新丢失它的数据时,它得到相关的和最近的数据。

    2019-12-04 14:23:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
继承与功能组合 立即下载
建立联系方法之一 立即下载
低代码开发师(初级)实战教程 立即下载