在UiView中添加UICollectionViewController

时间:2017-09-11 06:37:18

标签: ios iphone swift uiview uicollectionview

您是Ios开发的新手,我正在尝试使用imageViewpickerViewcollectionView开发一个简单的视图。

enter image description here

我有一个单独的UICollectionViewcontroller,我想在UIcollectionviewController的UiView中添加New MainVC。当我分别运行这两个UIViewControllers时,它们工作正常。但我不知道如何将UICollectionViewcontroller添加到VIView.Can有人帮助我将UICollectionviewcontroller置于UIView内或以任何建议以简单的方式执行此操作。

NewMainVC

import UIKit

class NewMainVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate  {

    @IBOutlet weak var DropDown: UIPickerView!
    @IBOutlet weak var textBox: UITextField!

    @IBOutlet weak var sensorCollection: UICollectionView!
    var values : [AnyObject] = []


    override func viewDidLoad() {
        super.viewDidLoad()
        self.values.append("Main Room" as AnyObject)
        self.values.append("Dinning Room" as AnyObject)
        self.values.append("Kitchen" as AnyObject)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.values.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let titleRow = (values[row] as? String)!
        return titleRow
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if values.count > 0 && values.count >= row{
            self.textBox.text = self.values[row] as? String
            self.DropDown.isHidden = true
        }
    }




    func textFieldDidBeginEditing(_ textField: UITextField) {
        //when you select the text field the picker view will be visible
        if(textField == self.textBox){
            self.DropDown.isHidden = false
            self.view.endEditing(true)
        }

    }

}

PhotosCollectionViewController

import UIKit
import SwiftyJSON

class PhotosCollectionViewController: UICollectionViewController
{
    @IBOutlet var home_collection_View: UICollectionView!


    var sensorObjectList = [SensorObject]()

    struct Storyboard {
        static let photoCell = "PhotoCell"
        static let headerView = "HeaderView"
        static let showDetailSegue = "ShowDetail"

        static let leftAndRightPaddings: CGFloat = 2.0
        static let numberOfItemsPerRow: CGFloat = 3.0
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let collectionViewWidth = collectionView?.frame.width
       let itemWidth = (collectionViewWidth! - Storyboard.leftAndRightPaddings) / Storyboard.numberOfItemsPerRow

        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth+50)

        NetworkCall().requestUsingGetMethod(url: "http://122.168.50.5:8181/rest/sitemaps", completion: { response in
            print(response)

            let jsonResults = JSON(String: response)
            print("------------------------------")
            print("HomePage Link : \(jsonResults[0]["homepage"]["link"].stringValue)")
            print("leaf : \(jsonResults[0]["homepage"]["leaf"].stringValue)")
            print("label : \(jsonResults[0]["label"].stringValue)")
            print("name : \(jsonResults[0]["name"].stringValue)")
            print("link : \(jsonResults[0]["link"].stringValue)")
            print("------------------------------")
            self.getHomePageData(homeurl: jsonResults[0]["link"].stringValue as String!)
        })
    }

    func getHomePageData(homeurl: String){

        NetworkCall().requestUsingGetMethodDictionnary(url: homeurl, completion: { response in
            print(response)
            let jsonResults = JSON(String: response)
            // print(json8 )
            print("-------------------------------------------------------------------------------------------------")
            print("label : \(jsonResults["label"].stringValue)")
            //                    print("Inside Homepage Link : \(json8["homepage"]["link"].stringValue)")
            //                    print("Inside the Item array : \(json8["homepage"]["widgets"].arrayValue)")
            //                    print("Inside the Item array2 : \(json8["homepage"]["widgets"][0]["widgets"].arrayValue)")
            for result in jsonResults["homepage"]["widgets"][0]["widgets"].arrayValue {
                print("---------------------------------------------")
                print("Label :  \(result["label"].stringValue)")
                print("widgetId :  \(result["widgetId"].stringValue)")
                print("icon :  \(result["icon"].stringValue)")
                print("type :  \(result["type"].stringValue)")
                print("category :  \(result["item"]["category"].stringValue)")
                print("link :  \(result["item"]["link"].stringValue)")
                print("Item label :  \(result["item"]["label"].stringValue)")
                print("type :  \(result["item"]["type"].stringValue)")
                print("state :  \(result["item"]["state"].stringValue)")
                print("name :  \(result["item"]["name"].stringValue)")

                let sensor =  SensorObject()
                sensor.Label = result["label"].stringValue
                sensor.widgetId = result["widgetId"].stringValue
                sensor.icon = result["icon"].stringValue
                sensor.category = result["item"]["category"].stringValue
                sensor.link = result["item"]["link"].stringValue
                sensor.Itemlabel = result["item"]["label"].stringValue
                sensor.type = result["type"].stringValue
                sensor.state = result["item"]["state"].stringValue
                sensor.name = result["item"]["name"].stringValue
                self.sensorObjectList.append(sensor)
               }
            self.home_collection_View.reloadData()
        })     

    }

    // MARK: - UICollectionViewDataSource

//    override func numberOfSections(in collectionView: UICollectionView) -> Int {
//        return photoCategories.count
//    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return self.sensorObjectList.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.photoCell, for: indexPath) as! PhotoCell

        let sensor = self.sensorObjectList[indexPath.row]

        cell.imageName = "ic_home_white"
        cell.imageText = sensor.name
        cell.statusText = sensor.state
        cell.titleText = sensor.Itemlabel

        return cell
    }


    // MARK: - UICollectionViewDelegate

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


//        let category = self.photoCategories[indexPath.section]
//        let image = UIImage(named: category.imageNames[indexPath.item])
//        
//        self.performSegue(withIdentifier: Storyboard.showDetailSegue, sender: image)
    }


}

有人可以帮助我将UICollectionviewcontroller置于UIView内或任何建议中以简单的方式执行此操作。

1 个答案:

答案 0 :(得分:2)

您可以将容器视图添加到mainVC并将您的集合视图嵌入到该容器视图