2

The first two are working just fine but when the scroll horizontally to the third view that is table view the data is not getting populated. I am not what is going wrong in my code or it is not possible to achieve table view in scroll view?

The ScrollViewController code is mentioned below -

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

The PropertyTableVC is mentioned below -

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
    }

}
Saurabh
  • 745
  • 1
  • 9
  • 33
  • have you added delegate and DataSource to tableview?? – Wasim Malek Dec 28 '17 at 11:54
  • Yes. I have added both data source and delegate to table view. – Saurabh Dec 28 '17 at 11:56
  • @Saurabh Do like this -> `class ViewController : UIViewController ,UITableViewDelegate,UITableViewDataSource{` – dahiya_boy Dec 28 '17 at 12:03
  • By the way Im not seeing tableview anywhere – dahiya_boy Dec 28 '17 at 12:04
  • @dahiya_boy Ok I'll try it out in my code and let you know. I have created a table view. – Saurabh Dec 28 '17 at 12:06
  • I have already mentioned data source and delegate after UIViewController and also mentioned in the viewdidload() as follows :- myTableView.dataSource = self and myTableView.delegate = self – Saurabh Dec 28 '17 at 12:16
  • The scrollview does not know how big the tableview is probably the problem. See this answer it may help you as it does exactly what you are trying to do. You also might have more success with a stack view and they can have an intrinsic size. https://stackoverflow.com/questions/42684594/ios-uiscrollview-with-dynamic-content-using-containerview-step-by-step/42685952#42685952 – agibson007 Dec 28 '17 at 15:40
  • @agibson007 I checked the linked you have provided. Actually I have to use UIScrollView in horizontal direction. There are totally three view pages i.e. two are normal views and the third one is the UITableView which is xib file. I am trying to populate that table view using custom cell which is also a xib file. – Saurabh Dec 29 '17 at 05:27

1 Answers1

1

The data in the tableView is loaded only when you conform to UITableViewDataSource UITableViewdelegate.

Use this , and have numberOfRows() and cellForRowAtIndexPath() in the class.

you will definitely get the populated tableView.

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
Madhur
  • 1,056
  • 9
  • 14
  • This is part of the problem for sure but would still need sizing to work – agibson007 Dec 28 '17 at 15:50
  • @saurabh where is your class implementing the tableView delegate methods. – Madhur Dec 29 '17 at 04:29
  • I have added new code in my question please take a look @MadhurDiwan – Saurabh Dec 29 '17 at 05:15
  • `let v4 = v3.propertyTabel.dequeueReusableCell(withIdentifier: "Cell") as? PropertyCell v4?.propertyCodeLbl.text = prop.code v4?.addressLbl.text = prop.address1 ` If you have filled the data in PropertyTableVC, then what is this for? – Madhur Dec 29 '17 at 05:49
  • Even if I comment those lines it is not making any difference. – Saurabh Jan 01 '18 at 05:47