【发布时间】:2020-03-14 20:53:17
【问题描述】:
我正在尝试将子视图控制器添加到表视图控制器,并将同一个子视图添加到常规 UIViewController 并且它有效。然而,我对 UITableViewController 使用了基本相同的代码,并且视图显示在屏幕的左上角。子视图控制器的实现和子视图控制器的创建代码如下。 *此外,还有一个 UIButton 应该从父级中删除子级,但它也不起作用。
在Parent(UITableViewController)中实现Child(UIViewController)-
func setupNavBar() {
navigationItem.title = "Select Paper Item"
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "QuestionIcon"), style: .plain, target: self, action: #selector(handleQuestion))
}
@objc func handleQuestion() {
addChild(questionView)
view.addSubview(questionView.view)
questionView.didMove(toParent: self)
view.addSubview(dismissQuestionView)
dismissQuestionView.translatesAutoresizingMaskIntoConstraints = false
dismissQuestionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
dismissQuestionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
dismissQuestionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
dismissQuestionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
dismissQuestionView.addTarget(self, action: #selector(handleDismissQuestionView), for: .touchUpInside)
questionView.view.translatesAutoresizingMaskIntoConstraints = false
questionView.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
questionView.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
questionView.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
questionView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
questionView.answer.text = "Recycling is very important, however we must ensure that we are recycling properly. This is because throwing trash in the recycling bin can not only cause recycling facilities to spend unnecesary money, but can also cause items that are recyclable to be thrown in the trash. To make matters worse, every recycling facility has different guidelines for what materials they accept and don't accept. You can look up where different items should go on this page. The first way is to click the magnifying glass on the right which will allow you to automatically search for items by simply typing their name. The second way is search manually by first selecting the material of the item on this page."
}
@objc func handleDismissQuestionView() {
questionView.willMove(toParent: nil)
questionView.view.removeFromSuperview()
questionView.removeFromParent()
dismissQuestionView.removeFromSuperview()
}
创造孩子-
import UIKit
class QuestionView: UIViewController {
let answer = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(white: 0, alpha: 0.7)
setupAnswer()
}
func setupAnswer() {
answer.backgroundColor = .white
answer.textColor = .black
answer.layer.cornerRadius = 20
answer.font = UIFont(name: "AlNile", size: 14)
view.addSubview(answer)
positionAnswer()
}
func positionAnswer() {
answer.translatesAutoresizingMaskIntoConstraints = false
answer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
answer.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
answer.heightAnchor.constraint(equalToConstant: 300).isActive = true
answer.widthAnchor.constraint(equalToConstant: 300).isActive = true
}
}
带有扩展的整个视图控制器-
import Firebase
import FirebaseFirestore
import FirebaseStorage
class PaperMaterial: UIViewController {
var rowHeight: CGFloat = 100
var headerheight: CGFloat = 150
let questionView = QuestionView()
let dismissQuestionView = UIButton()
let tableView = UITableView()
var alphaHeaderArray = [AlphaHeader]()
var aData = [CellData]()
var bData = [CellData]()
var cData = [CellData]()
var dData = [CellData]()
var eData = [CellData]()
var fData = [CellData]()
var gData = [CellData]()
var hData = [CellData]()
var iData = [CellData]()
var jData = [CellData]()
var kData = [CellData]()
var lData = [CellData]()
var mData = [CellData]()
var nData = [CellData]()
var oData = [CellData]()
var pData = [CellData]()
var qData = [CellData]()
var rData = [CellData]()
var sData = [CellData]()
var tData = [CellData]()
var uData = [CellData]()
var vData = [CellData]()
var wData = [CellData]()
var xData = [CellData]()
var yData = [CellData]()
var zData = [CellData]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupNavBar()
let db = Firestore.firestore()
alphaHeaderArray =
[AlphaHeader.init(label: "A"), AlphaHeader.init(label: "B"),
AlphaHeader.init(label: "C"), AlphaHeader.init(label: "D"),
AlphaHeader.init(label: "E"), AlphaHeader.init(label: "F"),
AlphaHeader.init(label: "G"), AlphaHeader.init(label: "H"),
AlphaHeader.init(label: "I"), AlphaHeader.init(label: "J"),
AlphaHeader.init(label: "K"), AlphaHeader.init(label: "L"),
AlphaHeader.init(label: "M"), AlphaHeader.init(label: "N"),
AlphaHeader.init(label: "O"), AlphaHeader.init(label: "P"),
AlphaHeader.init(label: "Q"), AlphaHeader.init(label: "R"),
AlphaHeader.init(label: "S"), AlphaHeader.init(label: "T"),
AlphaHeader.init(label: "U"), AlphaHeader.init(label: "V"),
AlphaHeader.init(label: "W"), AlphaHeader.init(label: "X"),
AlphaHeader.init(label: "Y"), AlphaHeader.init(label: "Z")]
db.collection("paper").getDocuments { (snapshot, error) in
if let error = error {
print(error)
return
}
else {
for document in snapshot!.documents {
let databaseData = document.data()
let name = databaseData["name"] as? String ?? ""
let category = databaseData["category"] as? String ?? ""
let firstLetter = name.prefix(1)
let storageRef = Storage.storage().reference()
let imageRef = storageRef.child("Paper/\(name).jpg")
imageRef.getData(maxSize: 50 * 1024) { data, error in
if let error = error {
print(error)
return
}
else {
let image = UIImage(data: data!)
if firstLetter == "A" {
self.aData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "B" {
self.bData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "C" {
self.cData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "D" {
self.dData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "E" {
self.eData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "F" {
self.fData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "G" {
self.gData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "H" {
self.hData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "I" {
self.iData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "J" {
self.jData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "K" {
self.kData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "L" {
self.lData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "M" {
self.mData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "N" {
self.nData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "O" {
self.oData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "P" {
self.pData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "Q" {
self.qData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "R" {
self.rData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "S" {
self.sData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "T" {
self.tData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "U" {
self.uData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "V" {
self.vData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "W" {
self.wData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "X" {
self.xData += [CellData.init(image: image, message: name, category: category)]
}
else if firstLetter == "Y" {
self.yData += [CellData.init(image: image, message: name, category: category)]
}
else {
self.zData += [CellData.init(image: image, message: name, category: category)]
}
self.tableView.reloadData()
}
}
}
}
}
self.tableView.register(ItemCustomCell.self, forCellReuseIdentifier: "custom")
positionTableView()
}
func setupNavBar() {
navigationItem.title = "Select Paper Item"
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "QuestionIcon"), style: .plain, target: self, action: #selector(handleQuestion))
}
@objc func handleQuestion() {
addChild(questionView)
view.addSubview(questionView.view)
questionView.didMove(toParent: self)
view.addSubview(dismissQuestionView)
dismissQuestionView.translatesAutoresizingMaskIntoConstraints = false
dismissQuestionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
dismissQuestionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
dismissQuestionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
dismissQuestionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
dismissQuestionView.addTarget(self, action: #selector(handleDismissQuestionView), for: .touchUpInside)
questionView.view.translatesAutoresizingMaskIntoConstraints = false
questionView.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
questionView.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
questionView.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
questionView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
questionView.answer.text = "Recycling is very important, however we must ensure that we are recycling properly. This is because throwing trash in the recycling bin can not only cause recycling facilities to spend unnecesary money, but can also cause items that are recyclable to be thrown in the trash. To make matters worse, every recycling facility has different guidelines for what materials they accept and don't accept. You can look up where different items should go on this page. The first way is to click the magnifying glass on the right which will allow you to automatically search for items by simply typing their name. The second way is search manually by first selecting the material of the item on this page."
}
@objc func handleDismissQuestionView() {
questionView.willMove(toParent: nil)
questionView.view.removeFromSuperview()
questionView.removeFromParent()
dismissQuestionView.removeFromSuperview()
}
func positionTableView() {
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
extension PaperMaterial: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = ObjectInfo()
switch indexPath.section {
case 0:
let data = aData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 1:
let data = bData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 2:
let data = cData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 3:
let data = dData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 4:
let data = eData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 5:
let data = fData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 6:
let data = gData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 7:
let data = hData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 8:
let data = iData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 9:
let data = jData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 10:
let data = kData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 11:
let data = lData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 12:
let data = mData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 13:
let data = nData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 14:
let data = oData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 15:
let data = pData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 16:
let data = qData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 17:
let data = rData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 18:
let data = sData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 19:
let data = tData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 20:
let data = uData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 21:
let data = vData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 22:
let data = wData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 23:
let data = xData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
case 24:
let data = yData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
default:
let data = zData[indexPath.row]
vc.name = data.message
vc.objImage = data.image
}
vc.category = "paper"
navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let alphaHeader = Bundle.main.loadNibNamed("AlphaHeaderView", owner: self, options: nil)?.first as! AlphaHeaderView
alphaHeader.headerLabel.text = alphaHeaderArray[section].label
return alphaHeader
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return headerheight
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! ItemCustomCell
cell.messageView.font = UIFont(name: "ChalkboardSE-Bold",size: 20.0)
cell.messageView.textColor = .black
cell.itemImageView.leftAnchor.constraint(equalTo: cell.leftAnchor, constant: 75).isActive = true
switch indexPath.section {
case 0:
cell.itemImage = aData[indexPath.row].image
cell.message = aData[indexPath.row].message
case 1:
cell.itemImage = bData[indexPath.row].image
cell.message = bData[indexPath.row].message
case 2:
cell.itemImage = cData[indexPath.row].image
cell.message = cData[indexPath.row].message
case 3:
cell.itemImage = dData[indexPath.row].image
cell.message = dData[indexPath.row].message
case 4:
cell.itemImage = eData[indexPath.row].image
cell.message = eData[indexPath.row].message
case 5:
cell.itemImage = fData[indexPath.row].image
cell.message = fData[indexPath.row].message
case 6:
cell.itemImage = gData[indexPath.row].image
cell.message = gData[indexPath.row].message
case 7:
cell.itemImage = hData[indexPath.row].image
cell.message = hData[indexPath.row].message
case 8:
cell.itemImage = iData[indexPath.row].image
cell.message = iData[indexPath.row].message
case 9:
cell.itemImage = jData[indexPath.row].image
cell.message = jData[indexPath.row].message
case 10:
cell.itemImage = kData[indexPath.row].image
cell.message = kData[indexPath.row].message
case 11:
cell.itemImage = lData[indexPath.row].image
cell.message = lData[indexPath.row].message
case 12:
cell.itemImage = mData[indexPath.row].image
cell.message = mData[indexPath.row].message
case 13:
cell.itemImage = nData[indexPath.row].image
cell.message = nData[indexPath.row].message
case 14:
cell.itemImage = oData[indexPath.row].image
cell.message = oData[indexPath.row].message
case 15:
cell.itemImage = pData[indexPath.row].image
cell.message = pData[indexPath.row].message
case 16:
cell.itemImage = qData[indexPath.row].image
cell.message = qData[indexPath.row].message
case 17:
cell.itemImage = rData[indexPath.row].image
cell.message = rData[indexPath.row].message
case 18:
cell.itemImage = sData[indexPath.row].image
cell.message = sData[indexPath.row].message
case 19:
cell.itemImage = tData[indexPath.row].image
cell.message = tData[indexPath.row].message
case 20:
cell.itemImage = uData[indexPath.row].image
cell.message = uData[indexPath.row].message
case 21:
cell.itemImage = vData[indexPath.row].image
cell.message = vData[indexPath.row].message
case 22:
cell.itemImage = wData[indexPath.row].image
cell.message = wData[indexPath.row].message
case 23:
cell.itemImage = xData[indexPath.row].image
cell.message = xData[indexPath.row].message
case 24:
cell.itemImage = yData[indexPath.row].image
cell.message = yData[indexPath.row].message
default:
cell.itemImage = zData[indexPath.row].image
cell.message = zData[indexPath.row].message
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return rowHeight
}
func numberOfSections(in tableView: UITableView) -> Int {
return 26
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return aData.count
}
if section == 1 {
return bData.count
}
if section == 2 {
return cData.count
}
if section == 3 {
return dData.count
}
if section == 4 {
return eData.count
}
if section == 5 {
return fData.count
}
if section == 6 {
return gData.count
}
if section == 7 {
return hData.count
}
if section == 8 {
return iData.count
}
if section == 9 {
return jData.count
}
if section == 10 {
return kData.count
}
if section == 11 {
return lData.count
}
if section == 12 {
return mData.count
}
if section == 13 {
return nData.count
}
if section == 14 {
return oData.count
}
if section == 15 {
return pData.count
}
if section == 16 {
return qData.count
}
if section == 17 {
return rData.count
}
if section == 18{
return sData.count
}
if section == 19 {
return tData.count
}
if section == 20 {
return uData.count
}
if section == 21 {
return vData.count
}
if section == 22 {
return wData.count
}
if section == 23 {
return xData.count
}
if section == 24 {
return yData.count
}
return zData.count
}
}
【问题讨论】:
-
当您使用 UITableViewController 时,已为您设置了委托和数据源。由于您切换到常规 UIViewController,因此您需要设置它们。这就是为什么您的 tableView 中没有数据的原因。