如何在Swift 4

时间:2017-10-05 14:54:55

标签: ios swift uitableview uicollectionview

这是我想要实现的:当我点击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  {

}

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

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

我认为您需要使用UICollectionViewController初始化init(collectionViewLayout: UICollectionViewLayout)collectionView必须设置布局,否则collectionView会在呈现时崩溃。

因此而不是:

let controller = UserProfileController()

尝试使用:

let controller = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())

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

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout  {

}