2017-02-26 102 views
0

我可以得到順着接下去的工作,但是沒有數據無論從收集/表的viewController的將傳遞到細節(我得到的只是白色的屏幕。)如何將數據從collectionView和tableView傳遞給detailViewController?

這是collectionViewController(名爲MemeCollectionViewController)

import UIKit 
import Foundation 


class SentMemesCollectionViewController: UICollectionViewController { 

    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout! 

    var _collectionView = SentMemesCollectionViewController!.self 


    //calling memes from array in Delegate 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    var memes: [Meme] { 
     return appDelegate.memes 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     collectionView?.reloadData() 

     flowLayoutSettings() 

    } 
    func flowLayoutSettings() { 

     let space: CGFloat = 3.0 
     let dimension = (self.view.frame.size.width - (2 * space))/3 

     flowLayout.minimumInteritemSpacing = space 
     flowLayout.minimumLineSpacing = space 
     flowLayout.itemSize = CGSize(width: dimension, height: dimension) 
    } 


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
       return memes.count 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: indexPath) as! MemeCollectionViewCell 

     let meme = self.memes[indexPath.item] 
     cell.getCellMeme(meme.memedImage) 

     return cell 
    } 

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "MemeDetailViewController" , 
     let nextScene = segue.destination as? MemeDetailViewController , 
      let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: IndexPath) { 
      let selectedMeme = memes[IndexPath] 

     } 
    } 

}

的tableViewController被命名爲MemeTableViewController

import UIKit 

class SentMemesTableViewController: UITableViewController { 

    var _tableView: UITableView! 
    var memeData: [Meme] = [] 



    //calling memes from array in Delegate 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    var memes: [Meme] { 
     return appDelegate.memes 

    } 

    override func viewWillAppear(_ animated: Bool) { 
     tableView.reloadData() 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.isScrollEnabled = true 

    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     if segue.identifier == "MemeDetailViewController" , 
     let _ = segue.destination as? MemeDetailViewController, 
      let indexPath = tableView.indexPathForSelectedRow { 
      let selectedMeme = memeData[indexPath.row] 
      _ = SentMemeImage(memedImage: selectedMeme.memedImage) 
     } 
    } 


    // MARK: - Table view data source 

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

      //navigationController!.pushViewController(MemeDetailViewController, animated: true) 

    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return memes.count 
    } 

    // Here it is! ----- 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "sentMemesTableView") as! MemeTableViewCell 
     let meme = memes[indexPath.row] 

     tableViewCell.tableViewImage.image = meme.memedImage 
     tableViewCell.tableViewLabel.text = "\(meme.topText)...\(meme.bottomText)" 



     return tableViewCell 
    } 

    // Override to support conditional editing of the table view. 
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 

    // Override to support conditional rearranging of the table view. 
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the item to be re-orderable. 
     return false 

    } 

    func deleteMemesInTableViewCell(_ index: Int) { 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     appDelegate.memes.remove(at: index) 
    } 

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if (editingStyle == UITableViewCellEditingStyle.delete) { 
      tableView.beginUpdates() 
      deleteMemesInTableViewCell(indexPath.row) 
      tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left) 
      tableView.endUpdates() 
     } 

    } 
} 

個繼承人是detailController,命名MemeDetailViewController

import UIKit 

class MemeDetailViewController: UIViewController { 

    var meme: SentMemeImage? 

    @IBOutlet weak var sentMemesBtn: UIBarButtonItem! 
    @IBOutlet weak var editBtn: UIBarButtonItem! 
    @IBOutlet weak var sentMemeView: UIImageView! 

    override func viewWillAppear(_ animated: Bool) { 
     displayMeme() 
    } 




    func displayMeme() { 
     self.sentMemeView.image = self.meme?.memedImage 

    } 

    @IBAction func launchMemeEditorViewController(_ sender: Any) { 
     _ = navigationController?.popViewController(animated: true) 

    } 

    //unwinding to the view before (the collectionView, or the tableView) 

    @IBAction func unwindVC(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) { 
     self.dismiss(animated: true, completion: nil) 
    } 
} 

回答

0

你需要用數據來更新目標 - 視圖 - 控制器(MemeDetailViewController)你想通過像第一個例子:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "MemeDetailViewController" , 
    let nextScene = segue.destination as? MemeDetailViewController , 
     let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: IndexPath) { 
     let selectedMeme = memes[IndexPath] 
     nextScene.meme = selectedMeme 

    } 
} 
+0

嘿,你能解釋一下如何更新目標視圖控制器? –

+0

您可以簡單地設置屬性,如上例所示: nextScene.meme = selectedMeme – chriskryn

0

你要吃創建的DetailView控制器的實例,然後指定其屬性所需的數據傳遞給它。

像:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ 

if (segue.identifier == "yourSegueIdentifer") { 
    // initialize new view controller and cast it as your view controller 
    var viewController = segue.destinationViewController as AnotherViewController 
    // your new view controller should have property that will store passed value 
     viewController.passedValue = valueToPass 
} 
} 
相關問題