2017-10-05 3 views
0

Hier ist, was ich erreichen möchte: Wenn ich ein UITabViewCell (Bild 1) antippen, wird es zu einem UICollectionViewController (Bild 2) navigieren. Genau wie die beiden folgenden Bilder zeigen:Wie von UITableViewCell zu UICollectionViewController in Swift programmgesteuert 4

Picture 1

Picture 2

ich zu einem UIViewController, wenn Hahn ein UITableViewCell navigieren können, aber es funktioniert nicht, wenn ich versuche zu UICollectionView zu navigieren. Hier sind meine Codes:

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 
    } 

} 

Die beiden Linien der Codes wird die App zum Absturz:

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

Hier wird die Fehlermeldung:

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

Hier werden die Codes des Ziel UICollectionViewController :

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

} 

Nach einiger Recherche habe ich keine brauchbaren Informationen gefunden. Gibt es eine Möglichkeit, dies zu erreichen? Oder gibt es eine Möglichkeit, das Bild 2 auf eine andere Weise zu implementieren, vielleicht: UIViewController statt UICollectionView?

Jeder Vorschlag wäre willkommen.

+0

Versuchen Sie ViewController von Storyboar zu machen d –

+1

Was ist die Fehlermeldung? –

+0

@YUNCHEN gibt es keine Fehlermeldung, einfach abstürzen. – user174782

Antwort

0

Ich glaube nicht, dass das Problem mit der Navigation ist, sollte jede Unterklasse von UIViewController navigierbar sein.

Ich glaube, Sie UICollectionViewController initialisieren müssen mit init(collectionViewLayout: UICollectionViewLayout), collectionView das Layout-Set haben muss, da sonst die collectionView stürzt ab, wenn es präsentiert wird.

Daher statt:

let controller = UserProfileController() 

versuchen Sie es mit:

let controller = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout()) 

Hier gehe ich davon aus, dass der folgende Code, um Ihre vollständige Umsetzung der UserProfileController ist, so dass Sie direkt initializers von UICollectionViewController erben:

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

} 
+0

Oh, Mann. Das ist das Problem. Ich danke dir sehr. Du hast mich gerettet. – user174782

+0

Ich bin auch schon mehrmals in diese Falle gefallen. Danke für die Aufwertung –

Verwandte Themen