cellForRowAt 在子表视图中不起作用。

huangapple go评论73阅读模式
英文:

cellForRowAt is not working in the childTableView

问题

我是一位初级的Swift开发者,正在开发一个个人应用程序,但在我的“tableViewController”中遇到了问题。

我有一个首个表格视图,从API中获取一些事件,然后我尝试在选择事件时显示“detailViewController”。
到目前为止一切似乎都很完美,但我的问题是我的“detailsviewcontroller”有一个事件的图片、标题... 然后一个集成的“tableview”来显示详细信息。在这一点上,numberOfRowsInSection函数有效,但cellForRowAt从未被调用来填充集成的tableview。
请帮助一下,希望我的解释清楚。

这是我想要显示事件详细信息的“childtableview”的代码(我只是使用数组characters来尝试):

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 {
        // 此指令会打印 4 
        print(characters.count)
        // 但它不会在tableview中返回 4 个单元格
        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()
        // 将新视图添加到主视图
        super.viewDidLoad()
        view.addSubview(newView)
        
        // 将其定位在事件图像和分段控制器下方
        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)
    }
}

希望这可以帮助解决你的问题。如果有其他问题,请随时提出。

英文:

I am an entry-level swift developer and I am working in a personal application but I am having an issue in my "tableViewController".

I have a first tableview where I fetch some events from an API, and then what I am trying to do is to show a "detailViewController" whenever I Select an event.
Until now everything seems perfect but my problem is my "detailsviewcontroller" has an image for the event, the title... and then an integrated "tableview" to show the details. At this point the numberOfRowsInSection function works but cellForRowAt never been called to fill the integrated tableview.
Any help please? I hope I am being clear in my explanation

This is the code of the "childtableview" where I want to show the event details( I am using the array characters just to try:

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

and here the code of the detailsViewController that contains the image and title and the child tableview:

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

得分: 0

1- 这不是添加包含的视图控制器的正确方式,请查看 https://stackoverflow.com/questions/27276561/adding-a-view-controller-as-a-subview-in-another-view-controller

2- 你需要给 newView 添加高度/底部约束。

英文:

1- This

views.append(ChildDetailsViewController().view)
views.append(ChildLocationViewController().view)

isn't the correct way to add contained vcs check https://stackoverflow.com/questions/27276561/adding-a-view-controller-as-a-subview-in-another-view-controller

2- You need to give a height/bottom constraint to newView

答案2

得分: 0

问题在于您创建了ChildDetailsViewController的实例并将其视图添加到views数组中,但是ChildDetailsViewController的实例从未保存在任何地方,也从未添加到父视图控制器中。并且newView.bringSubviewToFront(ChildDetailsViewController().view)这一行将不起作用,因为您正在创建一个新的实例并将其视图置于前台,但它从未添加到父视图中。

以下是更正的代码:

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

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

您已经创建了一个名为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)

请按照上述方式更正您的代码以确保正确添加和管理视图控制器及其视图。

英文:

The issue is you're creating an instance of ChildDetailsViewController and adding it's view to views array but ChildDetailsViewController's instance is never saved anywhere and never added it to parent view controller. and newView.bringSubviewToFront(ChildDetailsViewController().view) this line will do nothing cause you're creating a new instance and bringing its view to front but it's never been added to anywhere in 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

You've already created an instance childDetails you should use it like below

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)

huangapple
  • 本文由 发表于 2020年1月7日 02:44:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定