Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios cellForRowAt未在ChildTable视图中工作_Ios_Swift_Uitableview_Uisegmentedcontrol_Pass Data - Fatal编程技术网

Ios cellForRowAt未在ChildTable视图中工作

Ios cellForRowAt未在ChildTable视图中工作,ios,swift,uitableview,uisegmentedcontrol,pass-data,Ios,Swift,Uitableview,Uisegmentedcontrol,Pass Data,我是一名入门级swift开发人员,我在一个个人应用程序中工作,但我的“tableViewController”有一个问题 我有一个第一个tableview,在这里我从API获取一些事件,然后我试图做的是每当我选择一个事件时显示一个“detailViewController”。 到目前为止,一切似乎都很完美,但我的问题是我的“detailsviewcontroller”有一个活动的图像,标题。。。然后是一个集成的“tableview”来显示细节。此时numberOfRowsInSection函数起

我是一名入门级swift开发人员,我在一个个人应用程序中工作,但我的“tableViewController”有一个问题

我有一个第一个tableview,在这里我从API获取一些事件,然后我试图做的是每当我选择一个事件时显示一个“detailViewController”。 到目前为止,一切似乎都很完美,但我的问题是我的“detailsviewcontroller”有一个活动的图像,标题。。。然后是一个集成的“tableview”来显示细节。此时numberOfRowsInSection函数起作用,但从未调用cellForRowAt来填充集成表视图。 需要帮忙吗?我希望我的解释是清楚的

这是“childtableview”的代码,我想在其中显示事件详细信息(我使用数组字符只是为了尝试:

import UIKit
import SwiftyJSON

class ChildDetailsViewController: UIViewController, UITableViewDelegate {
    var event: JSON?
    let detailsTableView = UITableView()
    var characters = ["test1", "test2", "test3", "test4"]
    override func loadView() {
        print("details")
       super.loadView()
       view.addSubview(detailsTableView)
       detailsTableView.translatesAutoresizingMaskIntoConstraints = false
       detailsTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
       detailsTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
       detailsTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
       detailsTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
       detailsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
       detailsTableView.dataSource = self
       detailsTableView.delegate = self
       detailsTableView.reloadData()
    }
}

extension ChildDetailsViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //this insctruction does print 4 
        print(characters.count)
        //but it doesn't return 4 cells in the tableview
        return characters.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    print(characters[indexPath.row])
    cell.textLabel?.text = characters[indexPath.row]
    return cell
  }
}
这里是包含图像和标题以及子表视图的detailsViewController的代码:

import UIKit
import  SwiftyJSON

class EventDetailsViewController: UIViewController {
    var event : JSON?

    @IBOutlet weak var segmentedController: UISegmentedControl!
    @IBOutlet weak var ticketsView: UIView!
    @IBOutlet weak var locationView: UIView!
    @IBOutlet weak var detailsView: UIView!
    @IBOutlet weak var eventImage: UIImageView!
    @IBOutlet weak var eventTitle: UILabel!
    let newView = UIView()
    let childDetails = ChildDetailsViewController()
    var views : [UIView]!

    override func viewDidLoad() {
        filltheImageAndTile()
        // add new view to the main view
        super.viewDidLoad()
        view.addSubview(newView)

        //locate it under the event image and the segnented controller
        newView.translatesAutoresizingMaskIntoConstraints = false
        newView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        newView.topAnchor.constraint(equalTo: segmentedController.bottomAnchor, constant: 25).isActive = true
        newView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        newView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

        views = [UIView]()
        views.append(ChildDetailsViewController().view)
        views.append(ChildLocationViewController().view)

        for itemView in views {
            newView.addSubview(itemView)
        }
        newView.bringSubviewToFront(ChildDetailsViewController().view)
    }

    @IBAction func segmentedControlOptionChanged(_ sender: UISegmentedControl) {
        newView.bringSubviewToFront(views[sender.selectedSegmentIndex])
    }

    @IBAction func backButtonClicked(_ sender: UIBarButtonItem) {
        self.navigationController?.dismiss(animated: true)
    }
}
1-这个

views.append(ChildDetailsViewController().view)
views.append(ChildLocationViewController().view)
添加包含的vcs检查不是正确的方法吗


2-您需要为
newView

提供高度/底部约束问题是您正在创建
ChildDetailsViewController
的实例,并将其视图添加到
视图
数组中,但
ChildDetailsViewController
的实例从未保存在任何位置,也从未将其添加到父视图控制器中。并且
newView.bringSubviewToFront(ChildDetailsViewController().view)
这一行将不起任何作用,因为您正在创建一个新实例并将其视图置于前端,但它从未被添加到super view中的任何位置

views = [UIView]()
views.append(ChildDetailsViewController().view)//this shouldn't be like this
views.append(ChildLocationViewController().view)

for itemView in views {
    newView.addSubview(itemView)
}
newView.bringSubviewToFront(ChildDetailsViewController().view)//this shouldn't be like this
您已经创建了一个实例
childDetails
您应该像下面那样使用它

childDetails.willMove(toParent: self)
self.addChild(childDetails)

let locationViewController = ChildLocationViewController()
locationViewController.willMove(toParent: self)
self.addChild(locationViewController)

views = [UIView]()
views.append(childDetails.view)
views.append(locationViewController.view)

for itemView in views {
    newView.addSubview(itemView)
}
childDetails.didMove(toParent: self)
locationViewController.didMove(toParent: self)
newView.bringSubviewToFront(childDetails.view)

另外,正如@Sh_Khan所提到的,您应该负责为子视图控制器提供约束,使其能够适合
newView
properlyit解决我的问题,感谢您的澄清