UISegmentedControl包含UITableView和UICollectionView

时间:2016-04-01 04:48:20

标签: ios swift

我有一个ViewController包含1个SegmentedControl,它有三个段,TableView和CollectionView。选择段后,前两个段显示TableView,其中包含两个不同的自定义TableviewCell,最后一个显示CollectionView。选择每个segemet会调用网络函数来下载数据以填充TableView或CollectionView。该应用程序默认显示第一个段的TableView。问题是当我启动应用程序时,应用程序在numberOfItemsInSection函数中崩溃,因为它没有任何数据要填充。我认为这个函数被称为数据源,而CollectionView的委托属性被设置为' self'(ViewController)。我应该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:1)

好的,所以您可能正在将这些数据下载到JSON字典或数组中,因此您最初可以将表和集合视图设置为没有这样的内容:

//span[contains(text(), "U10 Boys")]//following-sibling::table/thead/tr/td[1]/table/tbody/tr/td[2]/span[contains(text(), "2014 Spring Recreation Soccer")]

然后,一旦您的数据进入,只需使用所述数据填充class ViewController : UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate { // note that I would recommend separating these into extensions on ViewController var htmlResponses = []() override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return htmlResponses.count // htmlResponses is empty at this point } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return htmlResponses.count // htmlResponses is empty at this point } } 数组,并分别为您的表格视图和集合视图调用htmlResponsestableView.reloadData()

即使您不使用数组来存储数据,概念也是一样的。使用空元素填充视图,并在知道数据从服务器到达后重新加载它们。