2个视图中的集合视图

时间:2018-05-29 11:48:12

标签: ios swift uicollectionview

我有这个StoryBoard: enter image description here 和MainViewControler中的代码:

class TipViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 
    @IBOutlet weak var collectionView1: UICollectionView!
    @IBOutlet weak var collectionView2: UICollectionView!

    let tips = ["Jakość frytek nas nie zadawala", "Kolor frytek jest niesatysfakcjonujący", "LOT i reklamacja", "Olej nie spełnia naszych oczekiwań", "jakiś fajny"]

    let tipsImages: [UIImage] = [UIImage(named: "a1.jpg")!, UIImage(named: "a2.jpg")!, UIImage(named: "a3.jpg")!, UIImage(named: "a4.jpg")!, UIImage(named: "a5.jpg")!]

    let leaflets = ["AV-AddedValueFries-Ulotka", "AV-AddedValueFries-Ulotka 23112", "Ulotka", "Fajna ulotka"]

    let leafletsImages: [UIImage] = [UIImage(named: "d1.jpg")!, UIImage(named: "d2.jpg")!, UIImage(named: "d3.jpg")!, UIImage(named: "d4.jpg")!]

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView1.dataSource = self
        collectionView1.delegate = self
        collectionView2.dataSource = self
        collectionView2.delegate = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tips.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView1.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as UICollectionViewCell
        cell.titleLabel.text = tips[indexPath.item]
        cell.imgView.image = tipsImages[indexPath.item]

        return cell
    }
}

CellVC1:

class TipCollectionViewCellLeaflets: UICollectionViewCell {
    // Outtlets
    @IBOutlet weak var imgView1: UIImageView!
    @IBOutlet weak var titleLabel1: UILabel!

}

CellVC2:

class TipCollectionViewCellLeaflets: UICollectionViewCell {
    // Outtlets
    @IBOutlet weak var imgView2: UIImageView!
    @IBOutlet weak var titleLabel2: UILabel!

}

我有问题: 1.排队:

cell.titleLabel.text = tips[indexPath.item]
        cell.imgView.image = tipsImages[indexPath.item]

我有错误:"类型的值' UICollectionViewCell'没有会员' titleLabel'"和"类型的值' UICollectionViewCell'没有会员' imgView'"

  1. 如何在第二个CollectionView(collectionView2)中显示数据?
  2. 更新1

    这段代码:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            if collectionView == collectionView1 {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! TipCollectionViewCellTips
                cell.titleLabel.text = tips[indexPath.item]
                cell.imgView.image = tipsImages[indexPath.item]
                return cell
            }
            else {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! TipCollectionViewCellLeaflets
                cell.titleLabel2.text = leaflets[indexPath.item]
                cell.imgView2.image = leafletsImages[indexPath.item]
                return cell
            }
        }
    

    编译没有问题。 现在问题在于此代码:

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return tips.count
        }
    

4 个答案:

答案 0 :(得分:1)

将numberOfItem方法更改为: -

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == collectionView1{
        return tips.count
    }else{
        return leaflets.count
}

将您的cellForItemAt方法更改为: -

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
           Var cell = UICollectionViewCell()  
    if collectionView == collectionView1{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as TipCollectionViewCellLeaflets
    }else{
       cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as TipCollectionViewCellLeaflets
    }
   cell.titleLabel.text = tips[indexPath.item]
   cell.imgView.image = tipsImages[indexPath.item]

    return cell
             }

答案 1 :(得分:1)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

 if collectionView == collectionView1 {

        let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1 identifier", for: indexPath) as! TipCollectionViewCellTips
        cell.titleLabel.text = tips[indexPath.item]
        cell.imgView.image = tipsImages[indexPath.item]

     // set other properties

        return cell
 } else {

       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2 identifier", for: indexPath) as! TipCollectionViewCellLeaflets
      cell.titleLabel.text = tips[indexPath.item]
     cell.imgView.image = tipsImages[indexPath.item]

     // set other properties
    return cell
}

答案 2 :(得分:1)

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

         if collectionView == collectionView1 {
             let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as TipCollectionViewCellTips
             cell.titleLabel1.text = tips[indexPath.item]
             cell.imgView1.image = tipsImages[indexPath.item]
             return cell
         }
         else {
             let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as TipCollectionViewCellLeaflets
             cell.titleLabel2.text = leaflets[indexPath.item]
             cell.imgView2.image = leafletsImages[indexPath.item]
             return cell
    }
}

试试这个..

答案 3 :(得分:0)

  
    

将此链接更改为

  
 let cell = collectionView1.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as UICollectionViewCell
  
    

  
let cell = collectionView1.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as TipCollectionViewCellLeaflets