2017-06-16 3 views
0

Ich habe eine UICollectionView, die in UITableViewCell ist. Ich habe 7 Knopf in UICollectionViewCell. Wenn ich Button1 drücke, sollten die Daten an einen anderen Viewcontroller übergeben werden, tut es aber nicht.So übergeben Sie Daten von UICollectionViewCell (in UITableviewCell) zu einem anderen UIViewcontroller Swift3

// TableviewCell

import UIKit 


class backgroundViewCell: UITableViewCell { 

var dayName : String! 
var audioFileURL : String! 
var tittle : String? 

// guided collectionView 
@IBOutlet var collectionView: UICollectionView! 


let data = ["1","2","3","4","5","6","7"] 

let images = ["circleblank","circleblank","circleblank","circleblank","circleblank","circleblank","circleblank"] 

@IBOutlet var Labeltittle: UILabel! 
@IBOutlet var LabelDetail: UILabel! 
@IBOutlet var iconview: UIImageView! 

extension backgroundViewCell : UICollectionViewDataSource, UICollectionViewDelegate { 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 7 
} 

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

    cell.Label.text = data[indexPath.row] 
    cell.ImageView.image = UIImage(named: images[indexPath.row]) 

    let bgColorView = UIView() 
    bgColorView.backgroundColor = UIColor.clear 

    var imageView : UIImageView 
    imageView = UIImageView(frame:CGRect(x :4.1,y : 3.9, width:  cell.ImageView.frame.size.width, height : cell.ImageView.frame.size.height)); 
    imageView.image = UIImage(named : "blankoval") 


    bgColorView.addSubview(imageView) 


    cell.selectedBackgroundView = bgColorView 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    print("Collection view at row \(collectionView.tag) selected index path \(indexPath.row)") 
    switch (indexPath.row) { 
    case 0: 

     dayName = "Day 1" 

     self.audioFileURL = musicUrlFile.Day1 
     break 
    case 1: 
    self.audioFileURL = musicUrlFile.Day2 
     dayName = "Day 2" 

     break 
    case 2: 
    self.audioFileURL = musicUrlFile.Day3 
     dayName = "Day 3" 

     break 
    case 3: 
    self.audioFileURL = musicUrlFile.Day4 
     dayName = "Day 4" 

     break 
    case 4: 

     self.audioFileURL = musicUrlFile.Day5 
     dayName = "Day 5" 

     break 
    case 5: 
     self.audioFileURL = musicUrlFile.Day6 
     dayName = "Day 6" 

     break 
    default: 
     self.audioFileURL = musicUrlFile.Day7 
     dayName = "Day 7" 
     break 

    } 

} 

} 
} 

// Tableviewcontroller

import UIKit 

class PlayerGuidedTableViewController: UITableViewController { 


var audioFileURL : String! 
var dayName : String! 




// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

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

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

    return 600 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "guidedPlayerCell") as! backgroundViewCell 

    cell.selectionStyle = .none 
    cell.playPauseButton.addTarget(self, action: #selector(PlayerGuidedTableViewController.playPauseButtonAction), for: .touchUpInside) 


    return cell 
} 



override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "MainPlayer" { 
     if let collectionCell: backgroundViewCell = sender as? backgroundViewCell { 
      if let collectionView: UICollectionView = collectionCell.superview as? UICollectionView { 
       if let playerVC = segue.destination as? playerGuidedViewController { 


        playerVC.Day = dayName 
        playerVC.UrlAudio = audioFileURL 
       } 
      } 
     } 
    } 
} 

// override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
//  if segue.identifier == "guidedMainPlayer" { 
//    
//   let playerVC = segue.destination as! playerGuidedViewController 

//   playerVC.Day = dayName 
//   playerVC.UrlAudio = audioFileURL 
//    
//    
//  } 
// } 
} 



} 

Antwort

0

Hier werde ich zeigen, wie die Daten zu übergeben von der tablecell zum viewcontroller: Grundsätzlich haben Sie Ihre passieren viewcontroller Instanz an die tablecell und dann können Sie auf alle Eigenschaften und Methoden von viewcontroller Instanz in der tablecell

zugreifen
import UIKit 


class backgroundViewCell: UITableViewCell { 
var vcInstance: PlayerGuidedTableViewController? 
var dayName : String! 
var audioFileURL : String! 
var tittle : String? 

// guided collectionView 
@IBOutlet var collectionView: UICollectionView! 


let data = ["1","2","3","4","5","6","7"] 

let images = ["circleblank","circleblank","circleblank","circleblank","circleblank","circleblank","circleblank"] 

@IBOutlet var Labeltittle: UILabel! 
@IBOutlet var LabelDetail: UILabel! 
@IBOutlet var iconview: UIImageView! 

extension backgroundViewCell : UICollectionViewDataSource, UICollectionViewDelegate { 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 7 
} 

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

    cell.Label.text = data[indexPath.row] 
    cell.ImageView.image = UIImage(named: images[indexPath.row]) 

    let bgColorView = UIView() 
    bgColorView.backgroundColor = UIColor.clear 

    var imageView : UIImageView 
    imageView = UIImageView(frame:CGRect(x :4.1,y : 3.9, width:  cell.ImageView.frame.size.width, height : cell.ImageView.frame.size.height)); 
    imageView.image = UIImage(named : "blankoval") 


    bgColorView.addSubview(imageView) 


    cell.selectedBackgroundView = bgColorView 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    print("Collection view at row \(collectionView.tag) selected index path \(indexPath.row)") 
    switch (indexPath.row) { 
    case 0: 

     dayName = "Day 1" 

     self.audioFileURL = musicUrlFile.Day1 
     break 
    case 1: 
    self.audioFileURL = musicUrlFile.Day2 
     dayName = "Day 2" 

     break 
    case 2: 
    self.audioFileURL = musicUrlFile.Day3 
     dayName = "Day 3" 

     break 
    case 3: 
    self.audioFileURL = musicUrlFile.Day4 
     dayName = "Day 4" 

     break 
    case 4: 

     self.audioFileURL = musicUrlFile.Day5 
     dayName = "Day 5" 

     break 
    case 5: 
     self.audioFileURL = musicUrlFile.Day6 
     dayName = "Day 6" 

     break 
    default: 
     self.audioFileURL = musicUrlFile.Day7 
     dayName = "Day 7" 
     break 

    } 

} 
vcInstance.dayName = dayName 
} 
} 

Sie können die Daten von Zelle zu dem Viewcontroller übergeben

import UIKit 

class PlayerGuidedTableViewController: UITableViewController { 


var audioFileURL : String! 
var dayName : String! 




// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

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

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

    return 600 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> backgroundViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "guidedPlayerCell") as! backgroundViewCell 
    cell.vcInstance = self 
    cell.selectionStyle = .none 
    cell.playPauseButton.addTarget(self, action: #selector(PlayerGuidedTableViewController.playPauseButtonAction), for: .touchUpInside) 


    return cell 
} 



override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "MainPlayer" { 
     if let collectionCell: backgroundViewCell = sender as? backgroundViewCell { 
      if let collectionView: UICollectionView = collectionCell.superview as? UICollectionView { 
       if let playerVC = segue.destination as? playerGuidedViewController { 


        playerVC.Day = dayName 
        playerVC.UrlAudio = audioFileURL 
       } 
      } 
     } 
    } 
} 

// override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
//  if segue.identifier == "guidedMainPlayer" { 
//    
//   let playerVC = segue.destination as! playerGuidedViewController 

//   playerVC.Day = dayName 
//   playerVC.UrlAudio = audioFileURL 
//    
//    
//  } 
// } 
} 



} 
+0

Danke für Ihre Antwort. Es funktioniert nicht wirklich. –

+0

Was machst du? –

+0

Was ist der Fehler aufgetreten? –

0

Sie audioFileURL und dayName Variablen innerhalb PlayerGuidedTableViewController Klasse nicht benötigen.

class PlayerGuidedTableViewController: UITableViewController { 

    var audioFileURL : String! //delete this 
    var dayName : String!  //delete this 
    ... 
    ... 
} 

Ersetzen Sie Ihre prepare(for:sender:) Methode mit diesem:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "MainPlayer" { 
     if let backgroundViewCell = sender as? backgroundViewCell { 
      if let playerVC = segue.destination as? playerGuidedViewController { 
       playerVC.Day = backgroundViewCell.dayName 
       playerVC.UrlAudio = backgroundViewCell.audioFileURL 
      } 
     } 
    } 
} 
+0

Danke für Antworten. Es funktioniert nicht wirklich. –

+0

Was ist das Problem, das es erzeugt? – nayem

+0

Schwerwiegender Fehler: unerwarteterweise wurde nil beim Entpacken eines optionalen Werts im Next-View-Controller gefunden. –

Verwandte Themen