ios - 是否可以在 UIScrollView 中添加 UITableView?

标签 ios swift uitableview uiscrollview

前两个工作正常,但是当水平滚动到第三个 View ( TableView )时,数据没有被填充。我不是我的代码出了什么问题或者无法在 ScrollView 中实现 TableView ?

下面提到了ScrollViewController的代码——

ScrollView Controller

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
    }

}

最佳答案

只有当您符合UITableViewDataSource UITableViewdelegate 时,tableView 中的数据才会被加载。

使用这个,并且在类中有 numberOfRows()cellForRowAtIndexPath()

你肯定会得到填充的tableView

关于ios - 是否可以在 UIScrollView 中添加 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48006985/

相关文章:

iOS:在 xml 解析完成之前创建 TableView

ios - 滚动后表格 View 单元格困惑

ios - 当应用程序处于后台时,如何检查我的 UIDocument 是否已通过 iCloud 更新?

ios - 如何判断我的 Newsstand Atom Feed 是否会被处理?

swift - 在 UISwitch 发生变化时执行代码

arrays - Swift 正则表达式和分隔符

ios - 将 Z 格式的服务器日期转换为本地 nsdate

ios - 使用 UIAutomation 从文本字段中获取值

ios - Swift 中带有动画转场的漂亮用户界面

IOS Swift 如何更改 1 个表格 View 单元格