I currently have 6 views in one view controller with a button to select which view to show and hide.
@IBOutlet weak var liveScrollView: UIScrollView!
@IBOutlet weak var sevenDayCollectionView: UICollectionView!
@IBOutlet weak var fourTeenDayCollectionView: UICollectionView!
@IBOutlet weak var thirtyCollectionView: UICollectionView!
@IBOutlet weak var ninetyCollectionView: UICollectionView!
@IBOutlet weak var allDataCollectionView: UICollectionView!
Using the button, I control the selectedIndex, and the selectedIndex hides and shows.
var selectedIndex = 0 {
didSet {
switch selectedIndex {
case 0:
liveScrollView.isHidden = false
sevenDayCollectionView.isHidden = true
fourTeenDayCollectionView.isHidden = true
thirtyCollectionView.isHidden = true
ninetyCollectionView.isHidden = true
allDataCollectionView.isHidden = true
case 1:
liveScrollView.isHidden = true
sevenDayCollectionView.isHidden = false
fourTeenDayCollectionView.isHidden = true
thirtyCollectionView.isHidden = true
ninetyCollectionView.isHidden = true
allDataCollectionView.isHidden = true
case 2:
liveScrollView.isHidden = true
sevenDayCollectionView.isHidden = true
fourTeenDayCollectionView.isHidden = false
thirtyCollectionView.isHidden = true
ninetyCollectionView.isHidden = true
allDataCollectionView.isHidden = true
case 3:
liveScrollView.isHidden = true
sevenDayCollectionView.isHidden = true
fourTeenDayCollectionView.isHidden = true
thirtyCollectionView.isHidden = false
ninetyCollectionView.isHidden = true
allDataCollectionView.isHidden = true
case 4:
liveScrollView.isHidden = true
sevenDayCollectionView.isHidden = true
fourTeenDayCollectionView.isHidden = true
thirtyCollectionView.isHidden = true
ninetyCollectionView.isHidden = false
allDataCollectionView.isHidden = true
case 5:
liveScrollView.isHidden = true
sevenDayCollectionView.isHidden = true
fourTeenDayCollectionView.isHidden = true
thirtyCollectionView.isHidden = true
ninetyCollectionView.isHidden = true
allDataCollectionView.isHidden = false
default:
break
}
}
This seems like a very long way, is there any better way of doing this?
All I can think of is having all of the views in one scrollView and scrolling the content as the user changes the selectedIndex. Would that be the only and the best way to solve this?
CodePudding user response:
Various ways to write that - here's one:
var selectedIndex = 0 {
didSet {
let views: [UIView] = [
liveScrollView, sevenDayCollectionView,
fourTeenDayCollectionView, thirtyCollectionView,
ninetyCollectionView, allDataCollectionView,
]
for (i, v) in views.enumerated() {
v.isHidden = i != selectedIndex
}
}
}
You might even put that array at the class-level:
let views: [UIView] = [
liveScrollView, sevenDayCollectionView,
fourTeenDayCollectionView, thirtyCollectionView,
ninetyCollectionView, allDataCollectionView,
]
override func viewDidLoad() {
// normal stuff
}
and then shorten didSet
to:
var selectedIndex = 0 {
didSet {
for (i, v) in views.enumerated() {
v.isHidden = i != selectedIndex
}
}
}
The advantage of doing it that way is you may want to loop through that array of views elsewhere.