我是iOS编程新手。现在我不使用 storyboard
创建一个示例应用程序,我为滚动方向是垂直的主类实现了 UICollectionView
。我为水平方向添加了另一个 UICollectionView
的特定单元格。问题是 cell
水平滚动方向没有推送到我点击它的另一个 View Controller 。
这是我在 AppDelegate
中设置的 rootViewController
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let navigation = UINavigationController(rootViewController: CategoryController())
window?.rootViewController = navigation
这是我实现的 CategoryController
tableView
tableView.register(TableCell.self, forCellReuseIdentifier: cellId)
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
对于tableView
,当我想导航到特定的 Controller 时,当我点击特定的单元格时工作正常
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.item)
let view = DetailEachCellController()
navigationController?.pushViewController(view, animated: true)
}
问题是,在 DetailEachCellController
中,我使用 UICollectionView
列出一些数据,当我点击特定单元格时,没有导航到新 Controller
此处 DetailEachCellController
为 UIViewController
var collectionDetailView: UICollectionView!
collectionDetailView.register(DetailContactCell.self, forCellWithReuseIdentifier: cellIdContact)
collectionDetailView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdContent)
func setupCollectionView() {
collectionDetailView.topAnchor.constraint(equalTo: detailContent.topAnchor).isActive = true
collectionDetailView.widthAnchor.constraint(equalTo: detailContent.widthAnchor).isActive = true
collectionDetailView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
collectionDetailView.backgroundColor = .white
}
func callAction() {
print("pushing view")
let view = UIViewController()
self.navigationController?.pushViewController(view, animated: true)
}
这里是 UICollectionViewFlowLayout
的委托(delegate)
extension DetailEachCellController : UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContact, for: indexPath)
return cell
}
else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContent, for: indexPath)
cell.backgroundColor = .blue
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == 1 {
return CGSize(width: view.frame.width, height: 250)
}
return CGSize(width: view.frame.width, height: 120)
}
}
这里是 DetailContactCell
,我在其中添加了一些按钮供用户点击,并在下一个 View 中添加了更多按钮,但它并没有移动,而是调用了该函数。
class DetailContactCell: UICollectionViewCell {
var eachCellController: DetailEachCellController?
lazy var callButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.shadowOpacity = 0.25
button.backgroundColor = .white
button.setTitle("CALL", for: .normal)
button.layer.cornerRadius = 10
button.layer.shadowOffset = CGSize(width: 0, height: 10)
button.layer.shadowRadius = 10
return button
}()
lazy var bookButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.shadowOpacity = 0.25
button.backgroundColor = .white
button.layer.cornerRadius = 10
button.layer.shadowOffset = CGSize(width: 0, height: 10)
button.layer.shadowRadius = 10
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(callButton)
addSubview(bookButton)
callButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
callButton.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 15).isActive = true
callButton.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/2.5).isActive = true
callButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
bookButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
bookButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15).isActive = true
bookButton.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/2.5).isActive = true
bookButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
self.actionButton()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func actionButton() {
callButton.addTarget(self, action: #selector(callBtn), for: .touchUpInside)
}
@objc func callBtn() {
print("calling")
eachCellController = DetailEachCellController()
eachCellController?.callAction()
}
}
最佳答案
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContact, for: indexPath) as! DetailContactCell
// Here is line add to `UICollectionViewFlowLayout`
cell.eachCellController = self
return cell
}else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdContent, for: indexPath)
cell.backgroundColor = .blue
return cell
}
}
关于ios - 以编程方式从 CollectionView 单元推送 viewController 不起作用 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51475127/