使用两个 UICollectionView 在 UITableView 中显示不同的数据

Show diffrent data in UITableView with two UICollectionView

我有一个 UITableView 有一个 UICollectionView 和 我使用 numberOfRowsInSection 设置行数,使用 cellForRowAt 显示标题为:

的单元格
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return sectionsArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let strname = sectionsArray[indexPath.row]
    if (strname as AnyObject).isEqual(to: "top"){
        tableCellobj = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle

        tableCellobj.lblMiddle.text = "\(strname)"
        return tableCellobj
    }
    if (strname as AnyObject).isEqual(to: "bottom") {
        tableCellobj = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle

        tableCellobj.lblMiddle.text = "\(strname)"
        return tableCellobj
    }

如您所见,数组 sectionsArray 是:

var sectionsArray: NSMutableArray = NSMutableArray()
sectionsArray.add("top")
sectionsArray.add("bottom")

现在 table 显示两行 headers "top" 和 "bottom"

我有两个不同的数据 NSArray 要在 CollectionView 中显示:

top = ["1","2","3"]
bottom = ["4","5","6"]

collectionView 有一个标签(在 Storyboard 中标识),所以我使用标签来定义视图和显示数据:

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

    if collectionView.tag == 2 {

        objCollectionViewCellMiddle = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle"
        , for: indexPath) as! CollectionViewCellmiddle

        objCollectionViewCellMiddle.lblMiddle.text = top[indexPath.row] as? String

        return objCollectionViewCellMiddle

    }

现在,TableView 显示两个 CollectionView,但它在两个 CollectionView

中打印来自 top 数组的相同数据

我要:

1st CollectionView ---> 显示 top 数组

第二个 CollectionView ---> 显示 bottom 数组

我玩过 indexPath.section,但无法让它正常工作,因为它总是打印“0”

嗯,我觉得问题出在这个函数中:

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

    if collectionView.tag == 2 {

        objCollectionViewCellMiddle = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle"
        , for: indexPath) as! CollectionViewCellmiddle

        objCollectionViewCellMiddle.lblMiddle.text = top[indexPath.row] as? String

        return objCollectionViewCellMiddle
    }
}

数据赋值显示:

objCollectionViewCellMiddle.lblMiddle.text = top[indexPath.row] as? String

我建议您通过添加标记代码来更改表格视图代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let strname = sectionsArray[indexPath.row]
    if (strname as AnyObject).isEqual(to: "top"){
        tableCellobj = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle

        Add this: tableCellobj.collectionView.tag = 1
        tableCellobj.lblMiddle.text = "\(strname)"
        return tableCellobj
    }
    if (strname as AnyObject).isEqual(to: "bottom") {
        tableCellobj = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle

        Add this: tableCellobj.collectionView.tag = 2
        tableCellobj.lblMiddle.text = "\(strname)"
        return tableCellobj
    }

}

然后:

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

    if collectionView.tag == 1 {

        objCollectionViewCellMiddle = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle"
        , for: indexPath) as! CollectionViewCellmiddle

        objCollectionViewCellMiddle.lblMiddle.text = top[indexPath.row] as? String

        return objCollectionViewCellMiddle
    }
    else {

        objCollectionViewCellMiddle = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle"
        , for: indexPath) as! CollectionViewCellmiddle

        objCollectionViewCellMiddle.lblMiddle.text = bottom[indexPath.row] as? String

        return objCollectionViewCellMiddle
    } 
}

好的,我认为我们需要做的是改变您的设置方式。首先,我们应该将支持数据源更改为值类型,例如 [String]:

MyViewControllerClass

class MyViewController: UIViewController {

    var sectionsArray: [String] = ["top", "bottom"]
    var topSection: [String] = ["1","2","3"]
    var bottomSection: [String] = ["4","5","6"]

    @IBOutlet weak var myTableView: UITableView!
    @IBOutlet weak var topCollectionView: UICollectionView!
    @IBOutlet weak var bottomCollectionView: UICollectionView!

}

然后,我们需要修改table视图和集合视图的数据源:

Table 查看数据源

extension MyViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionsArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let sectionName = sectionsArray[indexPath.section]

        switch sectionName {
        case "top":
            let tableCell = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle
            tableCell.lblMiddle.text = sectionName
            return tableCell
        case "bottom":
            let tableCell = tableView.dequeueReusableCell(withIdentifier: "cellmiddle", for: indexPath) as! TableCellMiddle
            tableCell.lblMiddle.text = sectionName
            return tableCell
        default:
            return UITableViewCell()
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return topSection.count
        case 1:
            return bottomSection.count
        default:
            return 0
        }
    }
}

在这里,在 cellForRow 方法中,您实际上可以减少一些重复代码,因为无论您在哪个单元格,您都在做同样的事情。

集合视图数据源

extension MyViewController: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch collectionView {
        case topCollectionView:
            return topSection.count
        case bottomCollectionView:
            return bottomSection.count
        default:
            return 0
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch collectionView {
        case topCollectionView:
            let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle", for: indexPath) as! CollectionViewCellmiddle
            collectionCell.lblMiddle.text = topSection[indexPath.row]
            return collectionCell
        case bottomCollectionView:
            let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncellmiddle", for: indexPath) as! CollectionViewCellmiddle
            collectionCell.lblMiddle.text = bottomSection[indexPath.row]
            return collectionCell
        default:
            return UICollectionViewCell()
        }
    }
}

这里我们正在做的是打开作为参数而不是您设置的标记传入的实际集合视图本身,这样我们就可以轻松地看到当前正在访问哪个集合视图。一旦我们知道这一点,我们就能够确定我们需要访问您的哪些支持数组,并可以相应地分配文本。

如果愿意,您还可以减少此方法中的一些重复逻辑。

This 是一个可以编译的游乐场(稍后我将添加功能来演示它,以便它显示在时间轴中)。我认为它将帮助您实现您想要做的事情。