这是我的代码,我有3个按钮,想要在UICollectionView
中显示UICollectionViewCell
XIB
,点击视图控制器中的按钮,重复使用collectionView次数?
class ShortTermPageViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate{
@IBOutlet weak var CollectionView: UICollectionView!
@IBOutlet weak var secondCollectionView: UICollectionView!
@IBOutlet weak var ExpView: UIView!
@IBOutlet weak var Anytime: UISearchBar!
@IBOutlet weak var Anywhere: UISearchBar!
@IBOutlet weak var Guest: UISearchBar!
@IBOutlet weak var ScrollView: UIScrollView!
@IBOutlet weak var firstViewBottomSpace: NSLayoutConstraint!
@IBOutlet weak var thirdViewTopSpace: NSLayoutConstraint!
@IBOutlet weak var anywhereTopSpace: NSLayoutConstraint!
@IBOutlet weak var secondViewTopSpace: NSLayoutConstraint!
@IBOutlet weak var anywhereAnytimebutton: UIButton!
@IBOutlet weak var hideshowbutton: UIButton!
@IBOutlet weak var searchImageclick: UIImageView!
@IBOutlet weak var BlankView: UIView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var collectionBottomSpace: NSLayoutConstraint!
@IBOutlet weak var blankViewBottomSpace: NSLayoutConstraint!
var tableData: [String] = ["home1","home2","home3","home4","home5","home6"]
var tableimages:[String]=["th-2.jpeg","th-3.jpeg","th-4.jpeg","th-5.jpeg","th-6.jpeg","th-6.jpeg"]
override func viewDidLoad() {
super.viewDidLoad()
// let frame = CGRect(origin: CGPoint.zero,size: CGSize(width:self.view.frame.width,height: 50))
//let layout = UICollectionViewFlowLayout()
//Registering xib in collectionView cell(Experience button)
self.CollectionView.register(UINib(nibName:"CollectionViewCell",bundle: nil), forCellWithReuseIdentifier:"CollectionCell1")
// Registering xib in table view(home button)
let nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "cell")
Anywhere.isHidden = true
Anytime.isHidden = true
Guest.isHidden = true
secondViewTopSpace.constant = 65
hideshowbutton.isHidden = true
thirdViewTopSpace.constant=70
//collectionBottomSpace.constant=350
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// For anywhere,anytime button click
@IBAction func hideShow(_ sender: Any) {
Anywhere.isHidden = false
Anytime.isHidden = false
Guest.isHidden = false
secondViewTopSpace.constant = 180
anywhereTopSpace.constant = 30
hideshowbutton.isHidden = false
anywhereAnytimebutton.isHidden = true
searchImageclick.isHidden = true
thirdViewTopSpace.constant=195
blankViewBottomSpace.constant=10
collectionBottomSpace.constant=41
}
// For hide button
@IBAction func hide(_ sender: Any) {
hideshowbutton.isHidden = true
Anywhere.isHidden = true
Anytime.isHidden = true
Guest.isHidden = true
anywhereAnytimebutton.isHidden = false
searchImageclick.isHidden = false
secondViewTopSpace.constant = 65
firstViewBottomSpace.constant=800
thirdViewTopSpace.constant=70
collectionBottomSpace.constant=100
}
//For Homes button click
@IBAction func Homes(_ sender: Any) {
BlankView.isHidden=false
tableView.isHidden=false
CollectionView.isHidden=true
secondCollectionView.isHidden=true
}
//Experience button click
@IBAction func Experience(_ sender: Any) {
BlankView.isHidden=false
tableView.isHidden=true
CollectionView.isHidden=true
secondCollectionView.isHidden=true
}
//Places button click
@IBAction func Places(_ sender: Any) {
BlankView.isHidden=false
tableView.isHidden=true
CollectionView.isHidden=false
secondCollectionView.isHidden=false
collectionBottomSpace.constant=45
}
//For Tab Homes by defalut
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TblCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! TblCell
cell.homeName.text = tableData[indexPath.row]
cell.homeImg.image = UIImage(named: tableimages[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row \(indexPath.row) selected")
}
////Set Size of table view cell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 287
}
// for collection view Home button
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return self.tableData.count
}
func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath)->UICollectionViewCell{
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:"CollectionCell1",for:indexPath)as! CollectionViewCell
cell.cellLabel.text=tableData[indexPath.item]
cell.cellImage.image=UIImage(named: tableimages[indexPath.item])
return cell
}
}