У меня есть два массива данных, что я пытаюсь переключаться между использованием UISegmentedControl
, то данные должны быть отображены в UICollectionView
. Поэтому в раскадровке у меня есть 2 сегмента для управления сегментами, чтобы они соответствовали двум массивам. В настоящее время данные не отображаются, и следующие операторы коммутатора относятся к умолчанию prinln
.UISegmentedControl с UICollectionView Ошибка с коммутатором Заявление
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var number: Int = 0
switch tabs.selectedSegmentIndex {
case 0: number = array1.count
case 1: number = array2.count
default: println("error counting array for number of items in section")
}
return number
}
Есть ли у кого-нибудь идеи, где я могу ошибаться? Это первый раз, когда я попытался использовать сегментированный элемент управления.
Вот мой код Просмотр контроллера:
@IBOutlet weak var tabs: UISegmentedControl!
@IBOutlet weak var collectionView: UICollectionView!
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var array1 = [Type1]()
var array2 = [Type2]()
func loadData() {
var error: NSError?
let request1 = NSFetchRequest(entityName: "Type1")
request1.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
self.array1 = moc?.executeFetchRequest(request1, error: &error) as! [Type1]
let request2 = NSFetchRequest(entityName: "Type2")
request2.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
self.array2 = moc?.executeFetchRequest(request2, error: &error) as! [Type2]
self.collectionView.reloadData()
}
override func viewWillAppear(animated: Bool) {
loadData()
}
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
var number: Int = 0
switch tabs.selectedSegmentIndex {
case 0: number = array1.count
case 1: number = array2.count
default: println("error counting array for number of items in section")
}
return number
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
// Configure the cell
var data1 = array1[indexPath.row]
var data2 = array2[indexPath.row]
switch tabs.selectedSegmentIndex {
case 0:
cell.titleLabel.text = self.data1[indexPath.row].title
case 1:
cell.titleLabel.text = self.data2[indexPath.row].title
default: println("error with switch statement for cell data")
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
}
добавьте полное сообщение об ошибке, и какая линия бросает ошибку, вы можете использовать 'Breakpoints' для отладки – Aladin
@Aladin проверить свои изменения, благодаря – RileyDev
Добавить текущее значение' selectedSegmentedIndex' в сообщение об ошибке, увидит, почему он нажимает 'default':' println ("tabs.selectedSegmentedIndex =" (tabs.selectedSegmentedIndex) ")' –