前两个工作正常,但是当水平滚动到表视图的第三个视图时,数据不会被填充。我不是我的代码出了什么问题,或者无法在滚动视图中实现表视图?
下面提到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
}
}
最佳答案
仅当您符合UITableViewDataSource
UITableViewdelegate
时,才会加载tableView中的数据。
使用this,并在类中包含numberOfRows()
和cellForRowAtIndexPath()
。
您肯定会得到填充的tableView
。