Reputation: 43
I actually have a view controller in which i am showing product attributes in a table view in the lower half of the View Controller and was showing product thumbnail in table view header. But then i realised that a product can have multiple thumbnails so i should add a collection view in upper half of view controller to show all those thumbnail (scrollable horizontally). I added both datasource and delegate for Collection view (UICollectionViewDataSource, UICollectionViewDelegate) and wrote functions to return number of section, number of rows and cellAtIndex but these functions are not called.
So my query is can i have both collection view and table view in same view controller? If yes, then how?
I am using iOS 8 SDK with swift
Upvotes: 3
Views: 2084
Reputation: 270
class ViewController: UIViewController {
@IBOutlet weak var collView: UICollectionView!
@IBOutlet weak var tblView: UITableView!
var arrMain = NSMutableArray()
var arrDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
var arrSunday = ["No Data Available"]
var arrMonday = ["1","2","3"]
var arrTuesday = ["A","B","C"]
var arrWednesday = ["a","b"]
var arrThursday = ["d","e","f"]
var arrFriday = ["5","6","7"]
var arrSaturdsay = ["X","y","z"]
override func viewDidLoad() {
super.viewDidLoad()
arrMain = (arrSunday as NSArray).mutableCopy() as! NSMutableArray
}
}
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrDays.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collView.dequeueReusableCell(withReuseIdentifier: "collCell", for: indexPath) as! collCell
cell.lblDays.text = arrDays[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 0 {
arrMain = (arrSunday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 1 {
arrMain = (arrMonday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 2 {
arrMain = (arrTuesday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 3 {
arrMain = (arrWednesday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 4 {
arrMain = (arrThursday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 5 {
arrMain = (arrFriday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 6 {
arrMain = (arrSaturdsay as NSArray).mutableCopy() as! NSMutableArray
}
tblView.reloadData()
}
}
extension ViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrMain.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tblCell
cell.lblName.text = arrMain[indexPath.row] as? String
return cell
}
}
class tblCell: UITableViewCell{
@IBOutlet weak var lblName: UILabel!
}
class collCell : UICollectionViewCell{
@IBOutlet weak var lblDays: UILabel!
}
Upvotes: 6