我有一个表视图(Netflix应用风格)内的集合视图。我遵循这个教程:https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/Swift:准备不传递数据
现在我想实现一个功能,只要我点击一个集合视图,它会传递员工的名字到下一个视图控制器。 这里是我的代码:
import UIKit
class CategoryRow: UITableViewCell, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var jobpost:JobPosition?
var employee: Employee?
var myname = ""
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return jobpost!.Employees.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! EmployeeCell
cell.employeephoto.image = UIImage(named: "spanner")
cell.employeename.text = jobpost?.Employees[indexPath.row].name
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let myname = jobpost?.Employees[indexPath.row].name
//print(myname)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let EmployeeDetailPage = segue.destination as? EmployeeDetailViewController
EmployeeDetailPage?.employeename = myname
print(myname)
}
现在我的问题是,整个PrepareForSegue功能实际上并没有运行。该应用程序运行正常没有任何错误,但数据只是没有通过? (即打印(myname)实际上没有打印任何东西)。
有什么我错过了吗?非常感谢。
这已经为它的答案http://stackoverflow.com/questions/42739412/send-data-from-tableviewcontroller-to-detailviewcontroller/42744464#42744464 你不需要使用'prepareForSegue'方法。只有使用'didSelectRowAtIndexPath'方法才能实现此目的。 –