带部分的 UiTableView 的数据 + UISegmentControl

Data for UiTableView with sections + UISegmentControl

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

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

我现在想在tableview中介绍sections。组织上述数据的最佳方式是什么,以便它也可以包含部分信息。

您可以创建

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"])
  ]
]