我正在尝试使用UISegmentedControl
切换的两个数据数据,然后数据将显示在UICollectionView
中。因此,在故事板中,我有两个段用于段控制以匹配两个阵列。目前,数据未显示,以下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
}
答案 0 :(得分:2)
在viewDidLoad
将selectedSegmentedIndex
设置为0(或1),因为默认值为-1。
tabs.selectedSegmentedIndex = 0
默认值为UISegmentedControlNoSegment(未选择任何片段),直到用户触摸片段。将此属性设置为-1可关闭当前选择。
答案 1 :(得分:0)
对我来说什么都没有,但是尝试一下。 以编程方式添加UISegmentedControl时,只需在viewDidLoad方法中调用此行代码 UISegmentedControl操作名称,然后添加您的segmentedTabar变量名称或IBOutlet名称,如下所示:
segmentedTabarButtonsTap(segmentedTabbar)
segmentedTabarButtonsTap是segmentedTabbar动作, segmentedTabbar是segmentedTabbar变量名称