ios - UISegmentedControl 与 UICollectionView 错误与 Switch 语句

标签 ios swift switch-statement uicollectionview uisegmentedcontrol

我有两个数据数组,我试图使用UISegmentedControl在它们之间切换,然后数据将显示在UICollectionView。所以在 Storyboard 中,我有 2 个段用于段控件以匹配两个数组。目前数据未显示,以下 switch 语句属于默认 prinln

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
}

有没有人知道我可能哪里出错了?这是我第一次尝试使用分段控件。

这是我的 View Controller 代码:

@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


}

最佳答案

在您的 viewDidLoad 中,将 selectedSegmentedIndex 设置为 0(或 1),因为默认值为 -1。

tabs.selectedSegmentedIndex = 0

The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.

关于ios - UISegmentedControl 与 UICollectionView 错误与 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31598282/

相关文章:

ios - 如何以编程方式将视频从 iPhone 应用程序上传到 youtube?

ios - HomeKit - 当我从后台回到前台时,如何更新配件的 isReachable 值?

swift - 确实需要有关 UITouch(Sprite Kit)的帮助

f# - 在 F# 中,匹配表达式 'fall through' 可以匹配另一个吗?就像在 C# 或 C++ 中一样?

java - switch 中的所有情况都会执行,而只有一种情况应该被执行

recursion - 正确的 Rust 匹配习惯用法

ios - 在 Swift 中将数据传递给另一个 ViewController

ios - 每当我尝试使用 rangeOfString 时,它都会说在 UITextView 中找不到成员

ios - 核心数据提取在第二次使用谓词时返回 0 个结果

swift - 在 alamofire 中发送数组的数组