将 table 视图(行 select)的数据传递给视图控制器
Passing data of table view (row select) to view controller
mi 问题是这样的:我想 select 第一行,第二行......视图控制器必须根据行显示信息 selected(label, images, etc ), 我尝试了很多东西,但都没有用。
我使用协议和这个:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF
cell.l2.text = hname[indexPath.row]
cell.l1.text = prename[indexPath.row]
cell.img1.image = imgs[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let selectedH = self.nin2[(indexPath as NSIndexPath).row]
self.delegate?.HabiSelected(selectedH)
}
谢谢
您应该以编程方式为您呈现的下一个视图控制器创建实例,并将信息传递给该视图控制器,就像这样
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF
let nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("your identifier") as! YourCustomViewController
nextVC.yourVariable = cell.theInfoYouWhatToPass
self.presentViewController(nextVC, animated: true, completion: nil)
}
现在您的 nextVC 实例将在您的变量中存储您需要的信息
好的,我将展示如何使用静态字符串数组执行此操作。对其进行分析,使您的代码符合您的需要(图片,文本)
第 1 步: 创建新项目并 select viewcontroller goto -> Embed in-> Navigation Controller.然后把UITableView
加给你ViewController
。然后添加一个原型单元并命名为(eg.cell).
第 2 步: 创建新 UITableViewCell
(newcell) cocoa 触摸 class 文件。
步骤 3: 然后转到 identity->custom class -> add created UITableViewCell
cocoa touch class file to your prototype cell(cell).然后将 UILabel
添加到您的原型单元格。
第 4 步: 创建 table 视图委托以查看控制器并为标签创建插座到 newcell,然后 table 视图到视图控制器。
第 5 步: 将新的 UIViewController
添加到您的 collectionView 故事板,并遵循与 UITableView
类似的方法
第 6 步: 创建从 viewcontroller 到集合 viewcontroller 的推送 Segue 并命名该 segue。
之后,将以下代码添加到具有 table 视图的视图控制器中。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet var tableView: UITableView! //Outlets of table View
var titles = ["Michael Jackson","Taylor Swift","Katy Perry","Avril Lavigne"] //static Array
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableCell
cell.TitlesLabel.text = titles[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("collectionSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let index = self.tableView.indexPathForSelectedRow
let indexNumber = index?.row //0,1,2,3
let vc = segue.destinationViewController as! collectionViewController
vc.title = titles[indexNumber!] //NavigationItem.title on collectionViewController
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
将以下代码添加到您的集合视图控制器。
import UIKit
class collectionViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate{
@IBOutlet var collectionView: UICollectionView!
var collectionTitles = ["Hi","Hello","Hola","Ni hao"]
var taylorSwift = ["Speak Now","Red","Love Story","Blank Space"]
var thirdlabel = ["Jack","Sparrow","William","Mohan Singh"]
var fourthlabel = ["Namasta","Vanakkam","Tamil","Hindi"]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return taylorSwift.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! CollectionViewControllerCell
let identity = navigationItem.title
print(title)
if identity == "Michael Jackson"
{
cell.collectionLabel.text = collectionTitles[indexPath.row]
}
else if identity == "Taylor Swift"
{
cell.collectionLabel.text = taylorSwift[indexPath.row]
}
else if identity == "Katy Perry"
{
cell.collectionLabel.text = thirdlabel[indexPath.row]
}
else
{
cell.collectionLabel.text = fourthlabel[indexPath.row]
}
return cell
}
}
mi 问题是这样的:我想 select 第一行,第二行......视图控制器必须根据行显示信息 selected(label, images, etc ), 我尝试了很多东西,但都没有用。
我使用协议和这个:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF
cell.l2.text = hname[indexPath.row]
cell.l1.text = prename[indexPath.row]
cell.img1.image = imgs[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let selectedH = self.nin2[(indexPath as NSIndexPath).row]
self.delegate?.HabiSelected(selectedH)
}
谢谢
您应该以编程方式为您呈现的下一个视图控制器创建实例,并将信息传递给该视图控制器,就像这样
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF
let nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("your identifier") as! YourCustomViewController
nextVC.yourVariable = cell.theInfoYouWhatToPass
self.presentViewController(nextVC, animated: true, completion: nil)
}
现在您的 nextVC 实例将在您的变量中存储您需要的信息
好的,我将展示如何使用静态字符串数组执行此操作。对其进行分析,使您的代码符合您的需要(图片,文本)
第 1 步: 创建新项目并 select viewcontroller goto -> Embed in-> Navigation Controller.然后把UITableView
加给你ViewController
。然后添加一个原型单元并命名为(eg.cell).
第 2 步: 创建新 UITableViewCell
(newcell) cocoa 触摸 class 文件。
步骤 3: 然后转到 identity->custom class -> add created UITableViewCell
cocoa touch class file to your prototype cell(cell).然后将 UILabel
添加到您的原型单元格。
第 4 步: 创建 table 视图委托以查看控制器并为标签创建插座到 newcell,然后 table 视图到视图控制器。
第 5 步: 将新的 UIViewController
添加到您的 collectionView 故事板,并遵循与 UITableView
第 6 步: 创建从 viewcontroller 到集合 viewcontroller 的推送 Segue 并命名该 segue。
之后,将以下代码添加到具有 table 视图的视图控制器中。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet var tableView: UITableView! //Outlets of table View
var titles = ["Michael Jackson","Taylor Swift","Katy Perry","Avril Lavigne"] //static Array
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableCell
cell.TitlesLabel.text = titles[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("collectionSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let index = self.tableView.indexPathForSelectedRow
let indexNumber = index?.row //0,1,2,3
let vc = segue.destinationViewController as! collectionViewController
vc.title = titles[indexNumber!] //NavigationItem.title on collectionViewController
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
将以下代码添加到您的集合视图控制器。
import UIKit
class collectionViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate{
@IBOutlet var collectionView: UICollectionView!
var collectionTitles = ["Hi","Hello","Hola","Ni hao"]
var taylorSwift = ["Speak Now","Red","Love Story","Blank Space"]
var thirdlabel = ["Jack","Sparrow","William","Mohan Singh"]
var fourthlabel = ["Namasta","Vanakkam","Tamil","Hindi"]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return taylorSwift.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! CollectionViewControllerCell
let identity = navigationItem.title
print(title)
if identity == "Michael Jackson"
{
cell.collectionLabel.text = collectionTitles[indexPath.row]
}
else if identity == "Taylor Swift"
{
cell.collectionLabel.text = taylorSwift[indexPath.row]
}
else if identity == "Katy Perry"
{
cell.collectionLabel.text = thirdlabel[indexPath.row]
}
else
{
cell.collectionLabel.text = fourthlabel[indexPath.row]
}
return cell
}
}