2017-06-16 22 views
0

私はUITableViewCellの内部にあるUICollectionViewを持っています。私はUICollectionViewCellに7つのボタンを持っています。 Button1を押すと、データは別のViewControllerに渡されますが、そうではありません。ここでUICollectionViewCell(UITableviewCell内)から別の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 
//    
//    
//  } 
// } 
} 



} 

答えて

0

私はviewcontrollertablecellからデータを渡す方法を示すつもりです: 基本的に、あなたはあなたのを渡す必要がありますviewcontrollerインスタンスをtablecellに追加し、viewcontrollerインスタンスからすべてのプロパティとメソッドにアクセスできます。tablecell

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 
} 
} 

あなたはPlayerGuidedTableViewControllerクラス内audioFileURLdayName変数を必要としないのViewController

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

感謝を。実際には動作していません。 –

+0

あなたは何をしていますか? –

+0

エラーが発生しましたか? –

0

にセルからデータを渡すことができます。

class PlayerGuidedTableViewController: UITableViewController { 

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

は、これであなたのprepare(for:sender:)メソッドを置き換えます:返信用

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

お返事ありがとうございます。実際には動作していません。 –

+0

それは何の問題ですか? – nayem

+0

致命的なエラー:次のView Controllerでオプションの値をアンラッピングしている間に予期せずnilが見つかりました。 –

関連する問題