2017-09-11 2 views
0

안녕하세요, Ios 개발에 익숙하며, imageView, pickerViewcollectionView으로 간단한보기를 개발하고 있습니다. UiView 안에 UICollectionViewController 추가하기

enter image description here

은 내가 New MainVC에 UIView의 내부에 그 UIcollectionviewController를 추가 할 별도의 UICollectionViewcontroller 있습니다. 이 두 가지를 실행하면 UIViewControllers이 분리되어 제대로 작동합니다. 그러나 나는 UICollectionViewcontroller을 VIView에 추가하는 방법을 모른다. 누군가 내 UICollectionviewcontrollerUIView 안에 넣을 수 있도록 도와 주거나 간단한 방법으로이 작업을 수행하도록 제안 할 수 있습니까?

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) 
    } 


} 

누군가가 나를 넣어 도와 드릴까요 내 간단한 방법으로이 작업을 수행 할 수있는 UIView 또는 제안 내부 UICollectionviewcontroller.

+0

에 컬렉션 뷰를 포함 할 수 있습니다. sensorCollection.delegate = self; 자기. sensorCollection.dataSource = self; viewDidLoad –

+0

에서 확인하십시오. https : //stackoverflow.com/questions/34649173/how-can-i-call-presentviewcontroller-in-uiview-class 또는 https://stackoverflow.com/questions/27276561/adding-a- view-controller-as-a-subview-in-another-view-controller –

답변

2

당신은 당신의 mainVC에 컨테이너보기를 추가하고 두 라인 자체를 추가 NewMainVC에서 그 컨테이너보기

+0

나는 containerView, Tnx 형제에 대해 몰랐다. 나는 그 문제를 해결했다. –

+0

행복한 코딩 ... :) –