2017-12-28 30 views
1

最初の2つはうまくいきましたが、テーブルビューの3番目のビューに水平にスクロールすると、データが入力されません。私のコードで何がうまくいかないのですか、スクロールビューでテーブルビューを実現することはできません。UIScrollViewにUITableViewを追加することはできますか?

ScrollViewControllerコードが下に記載されている -

ScrollViewController

import UIKit 

class ViewController: UIViewController { 

    var land = LandlordList() 
    var prop = PropertyList() 

    @IBOutlet weak var myScrollview: UIScrollView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 


     let v1:ContactLandlord = ContactLandlord(nibName: "ContactLandlord", bundle: nil) 
     let v2:ContactLandlord2 = ContactLandlord2(nibName: "ContactLandlord2", bundle: nil) 
     let v3: PropertyTableVC = PropertyTableVC(nibName:"PropertyTableVC", bundle: nil) 

     self.addChildViewController(v1) 
     self.myScrollview.addSubview(v1.view) 
     v1.didMove(toParentViewController: self) 

     self.addChildViewController(v2) 
     self.myScrollview.addSubview(v2.view) 
     v2.didMove(toParentViewController: self) 

     self.addChildViewController(v3) 
     self.myScrollview.addSubview(v3.view) 
     v3.didMove(toParentViewController: self) 

     self.myScrollview.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height) 

     var v2frame: CGRect = v1.view.frame 
     v2frame.origin.x = self.view.frame.width 
     v2.view.frame = v2frame 

     var v3frame:CGRect = v2.view.frame 
     v3frame.origin.x = self.view.frame.width * 2 
     v3.view.frame = v3frame 

     v1.code.text = land.code 
    v1.titlelbl.text = land.title 
     v1.fname.text = land.firstName 
     v1.mname.text = land.middleName 
     v1.lname.text = land.lastName 
     v1.salutation.text = land.salutation 
     v1.add1.text = land.address1 
     v1.add2.text = land.address2 
     v1.add3.text = land.address3 
     v1.city.text = land.city 
     v1.area.text = land.area 
     v1.cluster.text = land.cluster 

     v2.email.text = land.email 
     v2.aemail.text = land.aemail 
     v2.cemail.text = land.certificationEmail 
     v2.memail.text = land.certificationEmail 
     v2.contact.text = land.contact 
     v2.hcontact.text = land.homeNumber 
     v2.wcontact.text = land.workNumber 
     v2.fcontact.text = land.faxNumber 

     let v4 = v3.propertyTabel.dequeueReusableCell(withIdentifier: "Cell") as? PropertyCell 
     v4?.propertyCodeLbl.text = prop.code 
     v4?.addressLbl.text = prop.address1 
    } 
} 

PropertyTableVCを以下に記載されている -

をPropertyTableVC

import UIKit 
import Alamofire 

class PropertyTableVC: UITableViewController { 


    @IBOutlet var propertyTabel: UITableView! 

    let URL_Landlord_Property_List = "http://127.0.0.1/source/api/LandlordPropertyList.php" 
    var count: Int = 0 
    var landlordPropertyArray: [PropertyList]? = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     propertyTabel.dataSource = self 
     propertyTabel.delegate = self 
     fetchData() 
     let nibName = UINib(nibName: "PropertyCell", bundle:nil) 
     self.propertyTabel.register(nibName, forCellReuseIdentifier: "Cell") 
    } 

    func fetchData(){ 
     let urlRequest = URLRequest(url: URL(string: URL_Landlord_Property_List)!) 
     let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in 
      if error != nil{ 
       print(error!) 
       return 
      } 
      print(data!) 
      self.landlordPropertyArray = [PropertyList]() 
      self.count = (self.landlordPropertyArray?.count)! 
      do{ 
       let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject] 

       if let datafromjson = json["landlords_property_list"] as? [[String: AnyObject]] { 
        print(datafromjson) 
        for data in datafromjson{ 
         var property = PropertyList() 
         if let id = data["ID"] as? Int,let code = data["Code"] as? String, let address1 = data["Address"] as? String 
         { 
          property.id = id 
          property.code = code 
          property.address1 = address1 

         } 
         self.landlordPropertyArray?.append(property) 
        } 
        print(self.landlordPropertyArray) 
        DispatchQueue.main.async { 
         self.propertyTabel.reloadData() 
        } 
       } 

      }catch let error { 
       print(error) 
      } 
     } 
     task.resume() 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return (landlordPropertyArray?.count)! 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     // Configure the cell... 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PropertyCell 
     cell.propertyCodeLbl.text = self.landlordPropertyArray?[indexPath.item].code 
     cell.addressLbl.text = self.landlordPropertyArray?[indexPath.item].address1 
     return cell 
    } 

} 
+0

delegateとDataSourceをtableviewに追加しましたか? –

+0

はい。データソースとデリゲートの両方をテーブルビューに追加しました。 – Saurabh

+0

@Saurabhこのようにしてください - > 'クラスViewController:UIViewController、UITableViewDelegate、UITableViewDataSource {' –

答えて

1

テーブルビューのデータは、UITableViewDataSourceUITableViewdelegateに準拠している場合にのみロードされます。

これを使用し、クラスにはnumberOfRows()cellForRowAtIndexPath()があります。

間違いなく人口はtableViewになります。

+0

これは問題の一部ですが、依然としてサイズ変更が必要です – agibson007

+0

これは動作しません – Saurabh

+0

@saurabhここで、あなたのクラスはtableViewデリゲートメソッドを実装しています。 – Madhur

関連する問題