问题描述
我在表视图(Netflix App Style)中有一个集合视图.我跟着这个教程:https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/
I have got a collection view inside a table view (Netflix App Style). I followed this tutorial: https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/
现在我想实现一个功能,每当我点击一个集合视图时,它都会将员工的姓名传递给下一个视图控制器.这是我的代码:
Now I want to implement a function that whenever I click on a collection view, it will pass the employee's name to the next view controller.Here is my code:
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 函数实际上并没有运行.应用程序正常运行没有任何错误,但数据只是没有通过?(即 print(myname) 实际上没有打印任何内容).
Now my problem is, the whole PrepareForSegue function didn't actually run. The app runs normally without any error but the data just didn't pass through? (i.e. the print(myname) didn't actually print anything).
有什么我遗漏的吗?非常感谢.
Is there something I missed out? Very much appreciated.
推荐答案
步骤 1
在您的 CategoryRow 中创建一个全局变量
Step 1
Create a global variable in your CategoryRow
var myname: String!
步骤 2
更改 didSelectItemAt 函数,如下所示
Step 2
Change the didSelectItemAt func like below
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
myname = jobpost?.Employees[indexPath.row].name
performSegue(withIdentifier: "EmployeeDetailViewController", sender: myname)
}
步骤 3
在EmployeeDetailViewController中创建一个变量
Step 3
Create a variable in EmployeeDetailViewController
var employeename: String!
步骤 4
为您的转场提供一个标识符.在这里我给EmployeeDetailViewController"然后像下面这样更改 prepareForSegue 函数
Step 4
Give a identifier to your segue. Here I give "EmployeeDetailViewController"Then change the prepareForSegue func like below
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EmployeeDetailViewController" {
if let EmployeeDetailVC = segue.destination as? EmployeeDetailViewController {
EmployeeDetailVC.employeename = myname
}
}
}
这篇关于Swift:准备不传递数据的转场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!