【问题标题】:Can i have a CollectionView and a TableView in same View Controller我可以在同一个视图控制器中有一个 CollectionView 和一个 TableView
【发布时间】:2015-09-13 19:23:53
【问题描述】:

我实际上有一个视图控制器,其中我在视图控制器下半部分的表格视图中显示产品属性,并在表格视图标题中显示产品缩略图。但后来我意识到一个产品可以有多个缩略图,所以我应该在视图控制器的上半部分添加一个集合视图来显示所有这些缩略图(可水平滚动)。我为集合视图(UICollectionViewDataSource、UICollectionViewDelegate)添加了数据源和委托,并编写了返回部分数、行数和 cellAtIndex 的函数,但没有调用这些函数。

所以我的问题是我可以在同一个视图控制器中同时拥有集合视图和表视图吗?如果是,那么如何?

我正在使用带有 swift 的 iOS 8 SDK

【问题讨论】:

    标签: uitableview uicollectionview


    【解决方案1】:

    类视图控制器: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
    }
    

    }

    类 tblCell: UITableViewCell{

    @IBOutlet weak var lblName: UILabel!
    

    }

    类 collCell : UICollectionViewCell{

    @IBOutlet weak var lblDays: UILabel!
    

    }

    【讨论】:

    • 我将答案理解为“是”。谢谢。
    猜你喜欢
    • 2017-04-20
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多