一个视图控制器iOS中的两个集合视图

时间:2018-06-14 07:25:50

标签: ios swift uicollectionview

我在一个视图控制器中有两个集合视图,第一个集合视图有类CollectionCellA,imageA为UIImageView!和labelA为UILabel!。类似的第二个集合视图与类CollectionCellB与imageB和labelB。我尝试使用以下快速代码运行,但它只显示空白(白色)屏幕。

[TestMethod]
public async Task GetDataFromDataBase_Returns_True()
{
    // Arrange
    IApplication classUnderTest = new Application();

    // Act
    var result = await classUnderTest.GetDataFromDataBase();

    // Assert
    Assert.IsTrue(result);
}

3 个答案:

答案 0 :(得分:0)

对我而言,下面的代码就像一个魅力。你应该在cellForItemAt indexPath

中发送/赋值
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.CollectionViewA {
            return imageArroy.count
        }

        return imageArroyB.count

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.CollectionViewA {
            let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellA", for: indexPath) as! CollectionCellA
            // Set up cell
            cellA.lbl.text = labelA[indexPath.row]

            return cellA
        }

        else {
            let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellB", for: indexPath) as! CollectionCellB
            // ...Set up cell
            cellB.lbl.text = labelB[indexPath.row]

            return cellB
        }
    }
}


class CollectionCellA : UICollectionViewCell {
    @IBOutlet weak var lbl: UILabel!

}

class CollectionCellB : UICollectionViewCell {
    @IBOutlet weak var lbl: UILabel!

}

答案 1 :(得分:0)

此代码适用于带有图像和标签的One View Controller中的两个集合视图。

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.CollectionViewA {
            return imageArroy.count
        }

        return imageArroyB.count 

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.CollectionViewA {
            let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellA", for: indexPath) as! CollectionCellA
            cellA.imageA.image = imageArroyB[indexPath.row]
            cellA.labelA.text = labelA[indexPath.row]
            // Set up cell
            return cellA
        }

        else {
             let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellB", for: indexPath) as! CollectionCellB
            cellB.imageB.image = imageArroyB[indexPath.row]
            cellB.labelB.text = labelB[indexPath.row]
            // ...Set up cell

            return cellB
        }
}

答案 2 :(得分:0)

  

添加两个集合视图,并将委托和数据源与view countroller连接,创建集合视图单元格,并与单元格和出口连接

@IBOutlet weak var collectionView2: UICollectionView!
@IBOutlet weak var collectionview1: UICollectionView!
var days = ["Sun","Mon","Tues","Wed","Thur","Frid","Sat"]
var dayTask = [String]()

var task = [["Sun1","Mon1","Tues1","Wed1","Thur1","Frid1","Sat1"],["Sun2","Mon2","Tue2","Wed2"],["Sun3","Mon3","Tues3","Wed3","Thur3","Frid3","Sat3"],["Sun4","Mon4"],["Sun5","Mon5","Tues5","Wed5","Thur5","Frid5","Sat5"],["Sun6","Mon6","Tues6","Wed6"],["Sun7","Mon7","Tues7","Wed7","Thur7"]]
override func viewDidLoad() {
    super.viewDidLoad()
    dayTask = task[0]
    // Do any additional setup after loading the view.
}
  

集合视图数据源和委托

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == collectionview1{
        return days.count
    }else{
       return dayTask.count
    }

}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell1", for: indexPath) as! CollectionViewCell1

    if collectionView == collectionview1{
        cell.label.text = days[indexPath.row]
        let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
        collectionViewLayout?.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 40)
        collectionViewLayout?.invalidateLayout()
    }else
    {
        let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
        collectionViewLayout?.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 0, right: 40)
        collectionViewLayout?.invalidateLayout()
        cell.label.text = dayTask[indexPath.row]
    }
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == collectionview1{
        dayTask = task[indexPath.row]
        collectionView2.reloadData()
    }
}