gpt4 book ai didi

ios - 如何在 Swift 4 中以编程方式从 UITableViewCell 导航到 UICollectionViewController

转载 作者:行者123 更新时间:2023-11-30 12:06:47 27 4
gpt4 key购买 nike

这就是我想要实现的目标:当我点击 UITabViewCell(图 1)时,它将导航到 UICollectionViewController(图 2)。就像下面两张图所示:

Picture 1

Picture 2

点击 UITableViewCell 时我可以导航到 UIViewController,但当我尝试导航到 UICollectionView 时它不起作用。这是我的代码:

import UIKit

class RealUserProfileController: UIViewController, UITableViewDelegate, UITableViewDataSource {


private let me = ["Vincent-St"]
private let followers = ["58 Followers"]
private let myPhotos = ["New Followers"]
private let others = ["My likes", "Notifications", "Clear Caches", "Drafts"]
private let settings = ["Settings"]
private let share = ["Share with friends"]
private let images = ["Hearts.png", "Footprint.png", "Notifications.png", "Trash-Empty.png"]


private let sections = ["me", "myPhotos", "others", "settings", "share"]



override func viewDidLoad() {

super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Arial", size: 18)!, NSAttributedStringKey.foregroundColor: UIColor.black]
navigationItem.title = "Profile"
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: displayWidth, height: displayHeight), style: .grouped)


myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")

myTableView.dataSource = self

myTableView.delegate = self

self.view.addSubview(myTableView)

view.backgroundColor = UIColor(white: 1, alpha: 0.95)

myTableView.translatesAutoresizingMaskIntoConstraints = false
myTableView.topAnchor.constraint(equalTo: view.topAnchor, constant: -20).isActive = true
myTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

}

func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}



func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

if indexPath.section == 0 {
return 72
}
return tableView.rowHeight
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if indexPath.section == 0 {
let controller = UserProfileController()
self.navigationController?.pushViewController(controller, animated: true)
print("Value: \(me[indexPath.row])")
} else if indexPath.section == 1 {
print("Value: \(myPhotos[indexPath.row])")
} else if indexPath.section == 2 {
print("Value: \(others[indexPath.row])")
} else if indexPath.section == 3 {
let controller = ResultController()
navigationController?.pushViewController(controller, animated: true)
print("Value: \(settings[indexPath.row])")
} else if indexPath.section == 4 {
print("Value: \(share[indexPath.row])")
let shareText = "Share our app"
let shareImage = UIImage(named: "bell_unselected.png")
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [shareText, shareImage as Any], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
}



// return the number of cells each section.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return me.count
} else if section == 1 {
return myPhotos.count
} else if section == 2 {
return others.count
} else if section == 3 {
return settings.count
} else if section == 4 {
return share.count
} else {
return 0
}
}

// return cells

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "MyCell")
if indexPath.section == 0 {

cell.textLabel?.text = "\(me[indexPath.row])"
cell.detailTextLabel?.text = "\(followers[indexPath.row])"
cell.imageView?.image = #imageLiteral(resourceName: "profile_image")

} else if indexPath.section == 1 {
cell.textLabel?.text = "\(myPhotos[indexPath.row])"
cell.imageView?.image = #imageLiteral(resourceName: "icon_friends")
} else if indexPath.section == 2 {
cell.textLabel?.text = "\(others[indexPath.row])"
cell.imageView?.image = UIImage.init(named: images[indexPath.row])

} else if indexPath.section == 3 {
cell.textLabel?.text = "\(settings[indexPath.row])"
cell.imageView?.image = #imageLiteral(resourceName: "Icon_Settings")
} else if indexPath.section == 4 {
cell.textLabel?.text = "\(share[indexPath.row])"
cell.imageView?.image = #imageLiteral(resourceName: "Share")
}

cell.accessoryType = .disclosureIndicator

return cell
}

}

这两行代码将使应用程序崩溃:

       let controller = UserProfileController()
self.navigationController?.pushViewController(controller, animated: true)

这是错误消息:

Cannot convert value of type 'UserProfileController.Type' to expected argument type 'UIViewController'

以下是目标 UICollectionViewController 的代码:

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout  {

}

经过一番研究,我没有找到任何有用的信息。有什么办法让它发生吗?或者有没有办法以另一种方式实现图2,也许是:UIViewController,而不是UICollectionView?

如有任何建议,我们将不胜感激。

最佳答案

我认为问题不在于导航,UIViewController 的任何子类都应该是可导航的。

我相信您需要使用 init(collectionViewLayout: UICollectionViewLayout) 初始化 UICollectionViewControllercollectionView 必须设置布局,否则collectionView 如果出现的话将会崩溃。

因此,而不是:

let controller = UserProfileController()

尝试使用:

let controller = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())

这里我假设以下代码是您对 UserProfileController 的完整实现,因此您直接从 UICollectionViewController 继承初始化程序:

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout  {

}

关于ios - 如何在 Swift 4 中以编程方式从 UITableViewCell 导航到 UICollectionViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588723/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com