0

我有一个包含类别列表的 UITableView(在本例中,运动类型列表,即基于水上和陆地)

然后,此表中的每个单元格都有一个与嵌套 UICollectionView 中显示的运动类型相关联的运动列表。例如,水基将有冲浪和游泳。

想想 Netflix - 垂直滚动类别和水平滚动子类别。

这是示例 JSON(注意,显示的 JSON 仅用于描述结构。我实际上已将其存储在 RealmDb 中)

{
"category": [
    {
        "title": "Sport Listings",
        "category_content": [
            {
                "title": "Water Based",
                "category_content": [
                    {
                        "title": "Surfing"
                    }, {
                        "title": "Swimming"
                    }, {
                        "title": "Snorkelling"
                    }, {
                        "title": "Tombstoning"
                    }
                ]
            },
            {
                "title": "Land Based",
                "category_content": [
                    {
                        "title": "Football"
                    }, {
                        "title": "Rugby"
                    }
                ]
            }
        ]
    }
]
}

我在表格中正确填充了两个单元格(“water Based”和“Land Based”),但我现在无法理解如何将计数和数据解析到每个表格行的集合视图,以便我可以设置文本对于每个集合视图委托。

如果我只是将每个表行的集合视图计数设置为随机值,然后让每个单元格文本显示行索引,它显示的是正确的索引。我这样做是为了消除任何布局问题。

有没有人有任何指示?从我已经看到的示例中,嵌套的 UICollectionView 计数是硬编码的,而不是动态的,与委托内容相同(相同的图像/文本)

我没有发布任何代码,因为在我试图弄清楚它时它已经变得一团糟。如果需要,我可以将其剥离并放在这里。

4

1 回答 1

0

In my opinion a tableview should only be used when you want to take advantage of its main features: Recycling in order to support a large number of cells, and other features like search, alphabetizer, etc. I would rather use a vertical stack view and manage your collection views yourself, rather than having them in cells. I would imagine having your collection views recycled as you scroll would be a nightmare - because the same cell instance would be reused - but now it would have the wrong collection view in it, as you scroll down.

If you will only have less than 10 rows in this table view, I would use a Vertical orientation Stackview. You don't need cells or recycling. When I used table views for vertical layouts before, I ran into problems with state cleanup and bleeding of elements across recycled cells.

I would probably break out each collection view's delegate into a separate class.

To answer your main question: If you still want to use a tableview, set the reuseIdentifier to a unique value for each row, so recycling doesn't take place. And manage an array of data source objects, to assign to each of your collection views in cellForRow of your tableView method.

Another option is to have each cell be the dataSource for each collection view - so it's completely self contained, by making a custom UITableViewCell subclass that serves as both a datasource and UI container for your collection view. Then injecting a universal data model into that cell in cellForRow method (that would work for all types of collection views - regardless of data type).

I'd keep the datasources as separate objects though, and inject them into the cells, because to me a cell should just be a "shell" - a UI layout element and shouldn't hold the details of a dataSource (that way the dataSource can be re-used in other places too, not just that cell)

于 2019-02-20T18:22:30.633 回答