我有 5 个 Collection View 单元格,名称为“餐厅”、“商店”、“医疗”、“客厅”、“公园”等。所以现在我设置了 10 个 View Controller ,并将 TableView 放置在每五个 Controller 内,我调用 json 数据并在 TableView 中显示。我还有一个自定义单元格,我在其中用于所有 iew controlelr TableView
就像说,如果用户按Restaurant
,我将重定向到一个 View controlellr。在其中我调用了“baseurl/category_01”的网络服务。因此,我将从这个网址获取名称、地址、图像,并将显示在我的表格 View 中
但我知道这是错误的做法。如果我有 20 个 Collection View 单元格。那么如果我创建 20 个带有 20 个 TableView 的 View Controller 意味着这将不太好。
这是我在 swift 2.0 中的第一个应用程序。
有人可以建议我对我所有 10 个 Collection View 数据的单元格使用单一 View controlellr 和一个 TableView 吗?
但是我的 Collection View 单元格数据中的一件事是我从一个 json url 获取。其中每个数据都被指定为一个类别 id。例如:
[
{
category_id : 0
category_name : Restaurant
},
{
category_id : 1
category_name : park
},
{
category_id : 2
category_name : shops
},
{
category_id : 3
category_name : medical
},
]
就像我将在 TableView 中显示的 10 个单元格数据一样。因此,当我将一个 View Controller 与一个 TableView 一起使用时,我需要提及,`which indespath.row press 并且应该等于我的类别 id 和 shoyld 显示相关数据。
现在我在我的collectionviewcontroller.swift
中使用的是。我只是按下了哪个索引路径行,我只是导航到另一个 View Controller
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// You can use indexPath to get "cell number x", or get the cell like:
let cell = collectionView.cellForItemAtIndexPath(indexPath)
if((indexPath.row) == 0) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: ViewController2 = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
self.presentViewController(vc, animated: true, completion: nil)
print("Layout 1 clicked")
}
else if((indexPath.row) == 1) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: ViewController3Bank = storyboard.instantiateViewControllerWithIdentifier("ViewController3Bank") as! ViewController3Bank
self.presentViewController(vc, animated: true, completion: nil)
print("Layout 2 clicked")
}
那么如何使用单个 viewcontrolel1r TableView 做到这一点..请帮助我..
谢谢
最佳答案
将类别名称存储在数组或其他位置,每当单元格被推送时,您都可以通过从数组中选择它来调用特定类别,如下所示
categoryArray[indexPath.row]
然后,当即将完成到另一个 View 的转换时,您检查已选择的类别,并使用相关数据填充表格 View 。您可以设置如下结构:
func determineDataSource()
{
switch category
{
case "Restaurant":
print("restaurant")
.
.
.
}
关于ios - 对每个 Collection View 单元格使用单个 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36267662/