gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:59:06 25 4
gpt4 key购买 nike

前两个工作正常,但是当水平滚动到第三个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com