xcode 将表视图(行选择)的数据传递给视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38779724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:07:00  来源:igfitidea点击:

Passing data of table view (row select) to view controller

iosswiftxcodeuitableviewviewcontroller

提问by JuanCa2607

mi problem is this: I want to select the first, second... row and the view controller must show the information according to the row selected(label, images, etc), I trying a lot of thing but, not work.

我的问题是这样的:我想选择第一、第二...行,并且视图控制器必须根据所选行(标签、图像等)显示信息,我尝试了很多东西,但都不起作用。

I use protocol and this:

我使用协议和这个:

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

Thanks

谢谢

enter image description here

在此处输入图片说明

采纳答案by Rajamohan S

Okay i will show how to do this with static array of string. Analise it to make your code what you need(image,text)

好的,我将展示如何使用静态字符串数组执行此操作。分析它以使您的代码满足您的需求(图像,文本)

Step 1:Create the new project and select viewcontroller goto -> Embed in-> Navigation Controller. Then add the UITableViewto you ViewController. Then add one prototype cell and name it(eg.cell).

步骤1:创建新项目并选择viewcontroller goto -> Embed in-> Navigation Controller。然后添加UITableView到你ViewController。然后添加一个原型单元格并为其命名(例如单元格)。

Step 2:Create new UITableViewCell(newcell) cocoa touch class file.

第 2 步:创建新的UITableViewCell(newcell)可可触摸类文件。

Step 3:Then goto identity->custom class ->add created UITableViewCellcocoa touch class file to your prototype cell(cell). Then add UILabelto your prototye cell.

步骤3:然后转到身份->自定义类->将创建的UITableViewCell可可触摸类文件添加到您的原型单元格(单元格)。然后添加UILabel到您的原型单元格中。

Step 4:Create table view delegate to view controller and create outlets for label to newcell and then table view to view controller.

第 4 步:创建表视图委托以查看控制器并为标签创建出口到 newcell,然后创建表视图以查看控制器。

Step 5:Add new UIViewControllerto your story board for collectionView and follow similar method of UITableView

第 5 步:UIViewController为 collectionView添加新的故事板并遵循类似的方法UITableView

Step 6:Create push Segue from viewcontroller to collection viewcontroller and name the segue.

第 6 步:创建从 viewcontroller 到 collection viewcontroller 的 push Segue 并命名该 segue。

After,Add below code to your view controller that has table View.

之后,将以下代码添加到具有表视图的视图控制器中。

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{


@IBOutlet var tableView: UITableView! //Outlets of table View

var titles = ["Michael Hymanson","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()

}
}

Add below code to your collection view controller.

将以下代码添加到您的集合视图控制器。

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 = ["Hyman","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 Hymanson"
{
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
}
}

回答by LuKenneth

You should programmatically create the instance for the next view controller that you present and pass the information to that view controller like so

您应该以编程方式为您呈现的下一个视图控制器创建实例,并将信息传递给该视图控制器,如下所示

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

Now your nextVC instance will have the info you need stored in yourVariable

现在您的 nextVC 实例将您需要的信息存储在 yourVariable 中