我可以使用分段控件在容器视图内的子表视图控制器之间进行切换,但是由于某种原因,当我进入场景时,它不会显示第一个表视图控制器的单元格。我的段索引默认为0,因此它在第一个段上显示了突出显示,但是没有相应地显示单元格。 这是segmentedControl和容器视图的代码:
import UIKit
class BarMainViewController: UIViewController {
let barSegmentedControl: UISegmentedControl = {
let barItems = ["Newest", "Rewarding", "Saved"]
let segmentedControl = UISegmentedControl(items: barItems)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: #selector(selectionDidChange(_:)), for: .valueChanged)
return segmentedControl
}()
let containerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(barSegmentedControl)
view.addSubview(containerView)
setDefaultVC()
barSegmentedControl.addUnderlineForSelectedSegment()
containerView.topAnchor.constraint(equalTo: barSegmentedControl.bottomAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -49).isActive = true
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}
@objc func selectionDidChange(_ sender: UISegmentedControl) {
updateView()
barSegmentedControl.changeUnderlinePosition()
}
private func updateView() {
if barSegmentedControl.selectedSegmentIndex == 0 {
remove(asChildViewController: BarRewardingViewController())
remove(asChildViewController: BarSavedViewController())
add(asChildViewController: BarNewestViewController())
} else if barSegmentedControl.selectedSegmentIndex == 1 {
remove(asChildViewController: BarNewestViewController())
remove(asChildViewController: BarSavedViewController())
add(asChildViewController: BarRewardingViewController())
} else {
remove(asChildViewController: BarNewestViewController())
remove(asChildViewController: BarRewardingViewController())
add(asChildViewController: BarSavedViewController())
}
}
private func add(asChildViewController viewController: UITableViewController) {
// Add Child View Controller
addChildViewController(viewController)
// Add Child View as Subview
containerView.addSubview(viewController.view)
// Configure Child View
viewController.view.translatesAutoresizingMaskIntoConstraints = false
// Notify Child View Controller
viewController.didMove(toParentViewController: self)
}
private func remove(asChildViewController viewController: UITableViewController) {
// Notify Child View Controller
viewController.willMove(toParentViewController: nil)
// Remove Child View From Superview
viewController.view.removeFromSuperview()
// Notify Child View Controller
viewController.removeFromParentViewController()
}
private func setDefaultVC() {
containerView.addSubview(BarNewestViewController().view)
}
}
这是表视图控制器的代码:
import UIKit
class BarNewestViewController: UITableViewController {
override func viewDidLoad() {
tableView.register(serviceCell.self, forCellReuseIdentifier: "service")
super.viewDidLoad()
view.backgroundColor = mainBlue
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "service", for: indexPath)
}
}
class serviceCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let nameLabel: UILabel = {
let label = UILabel()
label.text = "NAME"
label.font = UIFont(name: "Avnier-Heavy", size: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setupViews() {
addSubview(nameLabel)
backgroundColor = lightBlue
nameLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
nameLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
}
}
答案 0 :(得分:0)
由于BarNewestViewController
子类UITableViewController
会自动遵循UITableViewDataSource
和UITableViewDelegate
协议,例如tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
。但是,它不会告诉tableView
附带的属性UITableViewController
是delegate
,而dataSource
是BarNewestViewController
。
tableView.dataSource = self
tableView.delegate = self