使用数千个可搜索对象填充表格视图

时间:2018-02-05 05:47:10

标签: arrays swift

我希望用数以千计的可搜索对象填充表格视图。我查看了UISearchController教程:https://www.raywenderlich.com/157864/uisearchcontroller-tutorial-getting-started。看起来可搜索的对象放在viewDidLoad方法中,但是如果我有成千上万的那些呢?这可能会在viewDidLoad方法上放置数千行。我调整了源代码并注意到我可以将candies数组放在一个单独的文件中,并将其声明为另一个名称,如candiesAndMore,并将下面的代码更改为candies = candiesAndMore。还有其他方法可以解决这个问题吗?

override func viewDidLoad() {
super.viewDidLoad()

...

candies = [
  Candy(category:"Chocolate", name:"Chocolate Bar"),
  Candy(category:"Chocolate", name:"Chocolate Chip"),
  Candy(category:"Chocolate", name:"Dark Chocolate"),
  Candy(category:"Hard", name:"Lollipop"),
  Candy(category:"Hard", name:"Candy Cane"),
  Candy(category:"Hard", name:"Jaw Breaker"),
  Candy(category:"Other", name:"Caramel"),
  Candy(category:"Other", name:"Sour Chew"),
  Candy(category:"Other", name:"Gummi Bear"),
  Candy(category:"Other", name:"Candy Floss"),
  Candy(category:"Chocolate", name:"Chocolate Coin"),
  Candy(category:"Chocolate", name:"Chocolate Egg"),
  Candy(category:"Other", name:"Jelly Beans"),
  Candy(category:"Other", name:"Liquorice"),
  Candy(category:"Hard", name:"Toffee Apple")]

...

}

1 个答案:

答案 0 :(得分:0)

您可以使用Core Data框架轻松完成。 在Core Data中为Candies创建一个实体,在那里存储信息,在ViewDidLoad()中获取糖果,然后使用。