具有部分+ UISegmentControl的UiTableView的数据

时间:2018-11-02 20:15:35

标签: ios swift uitableview multidimensional-array uisegmentedcontrol

我在视图控制器中有一个表视图和一个分段控件,以便当用户切换到另一个分段时,表视图根据分段索引重新加载数据。 与此相关的数据存储在这样的二维数组中

var data = [
        ["Soccer",       "Golf",      "Basketball",    "AmericanFootball",
         "Baseball",     "Tennis",    "Valleyball",    "Badminton"],
        ["Apple",        "Pear",      "Strawberry",    "Avocado",
         "Banana",       "Grape",     "Melon",         "Orange",
         "Peach",        "Kiwi"]
    ]

我现在想在表格视图中介绍各个部分。什么是组织以上数据的最佳方法,以便它也可以具有部分信息。

1 个答案:

答案 0 :(得分:1)

您可以创建

struct Item {
   let title:String
   let arr:[String]
}

然后

let data  = [ Item(title:"First",arr: ["Soccer","Golf","Basketball", "AmericanFootball","Baseball","Tennis","Valleyball","Badminton"]) , 
             Item(title:"Second",arr: ["Apple","Pear" ,"Strawberry","Avocado","Banana", "Grape","Melon","Orange","Peach", "Kiwi"])
]

为每个细分制作内容

let data : [[Item]] = [ [ Item(title:"Section 1 seg 1",arr: ["Soccer","Golf","Basketball", "AmericanFootball","Baseball","Tennis","Valleyball","Badminton"]) , 
             Item(title:"Section 2 seg 1",arr: ["Apple","Pear" ,"Strawberry","Avocado","Banana", "Grape","Melon","Orange","Peach", "Kiwi"])
   ] ,

 [ Item(title:"Section 1 seg 2",arr: ["Soccer","Golf","Basketball", "AmericanFootball","Baseball","Tennis","Valleyball","Badminton"]) , 
             Item(title:"Section 2 seg 2",arr: ["Apple","Pear" ,"Strawberry","Avocado","Banana", "Grape","Melon","Orange","Peach", "Kiwi"])
  ]
]