我有一个UICollectionView,它位于UITableViewCell中。我在UICollectionViewCell中有7个按钮。当我按下Button1时,数据应传递给另一个ViewController,但它不会。
// 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 :(得分:0)
在这里,我将展示如何将数据从tablecell
传递到viewcontroller
:
基本上,您必须将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
}
}
您可以将数据从单元格传递到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
//
//
// }
// }
}
}
答案 1 :(得分:0)
audioFileURL
课程内您不需要dayName
和PlayerGuidedTableViewController
个变量。
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
}
}
}
}