问题描述
所以现在我在两个不同的文件中有两个类。一个是用作大型机的视图控制器,另一个是用于填充大型机的视图(其中有几个)。我是编程新手,因此遇到了巨大的视图控制器问题,因此这是我改变这一问题的方式。 (请注意,代码在拆分为不同的文件之前按预期工作)
代码:
import UIKit
import SwiftUI
import MapKit
import DateScrollPicker
var collectionview: UICollectionView!
var collectionview2: UICollectionView!
var cellId = "Cell1"
var cellId2 = "Cell2"
class logbookController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UIGestureRecognizerDelegate,MKMapViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
var topCard: topCardDeck! //how I call the other views
var midCardBookVar: midCardBook! //how I call the other views
override func viewDidLoad() {
super.viewDidLoad()
topCard = topCardDeck(frame: CGRect.zero)
midCardBookVar = midCardBook(frame: CGRect.zero)
background.backgroundColor = .white
//add views
self.view.addSubview(background)
background.addSubview(topCard)
background.addSubview(midCardBookVar)
topCard.translatesAutoresizingMaskIntoConstraints = false
midCardBookVar.translatesAutoresizingMaskIntoConstraints = false
topCard.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
topCard.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
topCard.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
topCard.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
midCardBookVar.topAnchor.constraint(equalTo: self.view.topAnchor,constant: 1).isActive = true
midCardBookVar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
midCardBookVar.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
midCardBookVar.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
var cellHeight:CGFloat = CGFloat()
cellHeight = self.view.frame.height * 1/8
return cellHeight
}
func tableView(_ tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView,willdisplayHeaderView view: UIView,forSection section: Int) {
guard let header = view as? UITableViewheaderfooterView else { return }
header.clipsToBounds = true
}
//我试图使这段代码起作用:topCard.removeFromSuperview()和midCardBook.removeFromSuperview() 打印声明有效,所以我知道点击正在注册
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
return sections[section] as? String
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return fruit.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! JumpsTableViewCell
cell.backgroundColor = .clear
cell.altitude.text = "Alt: 12,000 ft."
cell.favoriteView.image = UIImage(named: "ProfileFavorite")
cell.number.image = UIImage(named: "Numb 7")
cell.numberLabel.text = "#"
cell.dropzoneName.text = "\(fruit[indexPath.row])"
cell.skydiveDate.text = DateFormatter.localizedString(from: NSDate() as Date,dateStyle: .short,timeStyle: .short)
return cell
}
}
///这是我分割部分代码的地方。如您所见,委托和数据源已设置为主控制器logbook-controller。
class MidCardDeckBook: UIView {
let topCardDates = UIView()
var LogbookController = logbookController()
var myTableView: UITableView!
@IBAction func cloSEOverlay() {
UIView.animate(withDuration: 0.5,animations: {
self.alpha = 0.0
}) { (_) in
self.removeFromSuperview()
}
}
override init(frame: CGRect){
super.init(frame: frame)
topCardDates.backgroundColor = UIColor.black.withAlphaComponent(0.85)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
layout.itemSize = CGSize(width: self.frame.width,height: 700)
layout.scrollDirection = .horizontal
collectionview = UICollectionView(frame: self.frame,collectionViewLayout: layout)
collectionview.dataSource = LogbookController
collectionview.delegate = LogbookController
collectionview.register(FreelancerCell.self,forCellWithReuseIdentifier: cellId)
collectionview.showsverticalScrollIndicator = false
collectionview.backgroundColor = UIColor.clear
let layout2: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout2.sectionInset = UIEdgeInsets(top: 0,right: 0)
layout2.itemSize = CGSize(width: self.frame.width,height: 700)
layout2.scrollDirection = .vertical
collectionview2 = UICollectionView(frame: self.frame,collectionViewLayout: layout2)
collectionview2.dataSource = LogbookController
collectionview2.delegate = LogbookController
collectionview2.register(FreelancerCell.self,forCellWithReuseIdentifier: cellId2)
collectionview2.showsverticalScrollIndicator = false
collectionview2.backgroundColor = UIColor.clear
myTableView = UITableView()
myTableView.register(JumpsTableViewCell.self,forCellReuseIdentifier: "Cell")
myTableView.dataSource = LogbookController
myTableView.delegate = LogbookController
myTableView.backgroundColor = .clear
myTableView.frame = CGRect(x: 0,y: 0,width: 200,height: 20)
topCardDates.roundCorners([.topLeft,.topRight],radius: 5.0)
self.addSubview(topCardDates)
topCardDates.addSubview(collectionview)
topCardDates.addSubview(collectionview2)
topCardDates.addSubview(myTableView)
topCardDates.translatesAutoresizingMaskIntoConstraints = false
collectionview.translatesAutoresizingMaskIntoConstraints = false
collectionview2.translatesAutoresizingMaskIntoConstraints = false
myTableView.translatesAutoresizingMaskIntoConstraints = false
topCardDates.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
topCardDates.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
topCardDates.heightAnchor.constraint(equalTo: self.heightAnchor,multiplier: 1).isActive = true
collectionview.topAnchor.constraint(equalTo: topCardDates.topAnchor,constant: 10).isActive = true
collectionview.leftAnchor.constraint(equalTo: topCardDates.leftAnchor,constant: 10).isActive = true
collectionview.widthAnchor.constraint(equalTo: topCardDates.widthAnchor,multiplier: 2/3).isActive = true
collectionview.heightAnchor.constraint(equalTo: topCardDates.heightAnchor,multiplier: 1/12).isActive = true
collectionview2.topAnchor.constraint(equalTo: topCardDates.topAnchor,constant: 10).isActive = true
collectionview2.rightAnchor.constraint(equalTo: topCardDates.rightAnchor,constant: 10).isActive = true
collectionview2.widthAnchor.constraint(equalTo: topCardDates.widthAnchor,multiplier: 1/6).isActive = true
collectionview2.heightAnchor.constraint(equalTo: topCardDates.heightAnchor,multiplier: 1/12).isActive = true
myTableView.topAnchor.constraint(equalTo: collectionview.bottomAnchor).isActive = true
myTableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
myTableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented ")
}
}
我有一个名为topCardClass的类似类,该类中嵌入了一堆uiview和按钮。同样,我可以从logbookcontroller中访问按钮操作选择器,因为print语句可以工作,但是一旦我尝试从控制器中添加或删除视图,它就无法工作。不过没有错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)