我实际上有一个视图控制器,我在视图控制器的下半部分的表视图中显示产品属性,并在表视图标题中显示产品缩略图。但后来我意识到产品可以有多个缩略图,所以我应该在视图控制器的上半部分添加一个集合视图来显示所有这些缩略图(水平滚动)。我为Collection视图(UICollectionViewDataSource,UICollectionViewDelegate)添加了数据源和委托,并编写了返回段数,行数和cellAtIndex的函数,但是没有调用这些函数。
所以我的查询是否可以在同一视图控制器中同时拥有集合视图和表视图?如果是,那怎么办?
我正在使用带有swift的iOS 8 SDK
答案 0 :(得分:0)
类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
}
}
扩展名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()
}
}
扩展名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!
}