Ios 如何以编程方式将UICollectionView添加到容器?
我是IOS开发新手 以下是我想要实现的 -在主视图中有一个两个容器 -在第一个容器中,添加UICollectionView -在第二个容器中,添加UITableView 这就是我到目前为止所做的代码 主要观点:Ios 如何以编程方式将UICollectionView添加到容器?,ios,swift,Ios,Swift,我是IOS开发新手 以下是我想要实现的 -在主视图中有一个两个容器 -在第一个容器中,添加UICollectionView -在第二个容器中,添加UITableView 这就是我到目前为止所做的代码 主要观点: class MainViewController:UIViewController { let itemView:UIView = { let itemContainerView = UIView() itemContainerView.backgroundColor
class MainViewController:UIViewController {
let itemView:UIView = {
let itemContainerView = UIView()
itemContainerView.backgroundColor = UIColor.lightGray
let collectionView = CollectionViewController()
itemContainerView.addSubview(collectionView.view)
itemContainerView.translatesAutoresizingMaskIntoConstraints = false
return itemContainerView
}()
let tableView:UIView = {
let tableContainerView = UIView()
tableContainerView.backgroundColor = UIColor.gray
tableContainerView.translatesAutoresizingMaskIntoConstraints = false
return tableContainerView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(itemView)
view.addSubview(tableView)
setupItemView()
setupTableView()
}
func setupItemView(){
itemView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
itemView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
itemView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2/3).isActive = true
itemView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
func setupTableView() {
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: itemView.rightAnchor).isActive = true
tableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3).isActive = true
tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
}
CollectionViewController:
class CollectionViewController:UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellId = "CellId"
var colView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 111, height: 111)
colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
colView.delegate = self
colView.dataSource = self
colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
colView.backgroundColor = UIColor.white
colView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(colView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = UIColor.brown
return cell
}
}
我可以看到我的主屏幕有两个容器,但看不到10个收集单元
有人能告诉我正确的方向吗?提前谢谢
已更新
当我将MainViewController设置为初始视图时,它工作正常。
但是,在我的项目中,MainViewController不是初始视图。
流程是;
1.用户登录
2.将显示带有按钮的仪表板
3.单击按钮将用户导航到MainViewController
这是我的仪表板课程
class DashboardController: UIViewController{
let orderBtn:UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor.init(red: 173/255, green: 184/255, blue: 255/255, alpha: 1)
button.layer.cornerRadius = 5
button.setTitle("Select Item" , for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(handleOrderNavigation), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(orderBtn)
setupOrderBtn()
}
func handleOrderNavigation() {
let mainViewController= MainViewController()
let mainViewNavController= UINavigationController(rootViewController: mainViewController)
self.present(mainViewNavController, animated: false, completion: nil)
}
}
尝试在加载视图后的某个时间点调用
reloadData
。您的代码工作正常。这是我从您的collectionView
代码工作中获得的输出
暗示
MainViewController
的声明错误MainViewController
使用推送至MainViewController
我怀疑collectionView在
viewDidLoad
中的帧是否正确。使用自动布局,或稍后设置框架。当您将CollectionViewController添加到itemView
@JamesP时,您也没有设置框架。如何使用autolayout使视图适合superview(容器)?如果我将MainView设置为初始视图,正如您所说,它工作正常。但是当我从另一个视图导航到主视图时,我看不到单元格。@J.GS.Han您是否正在将数据从其他VC传递到主VC??你能说出你的工作流程吗。您在UI/I中实际需要的内容目前没有传递任何数据。流程是:一旦用户登录,用户将看到主仪表板。如果用户单击一个按钮,用户将被导航到主视图。我已经用上一个视图更新了这个问题class@J.GS.Han首先,它的MainVC
不是MainView
,当你正确地写东西时,我总是感到困惑,因为它们之间有很多不同。