将表视图(行选择)的数据传递给视图控制器 [英] Passing data of table view (row select) to view controller

查看:23
本文介绍了将表视图(行选择)的数据传递给视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我使用协议和这个:

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

谢谢

推荐答案

好的,我将展示如何使用字符串的静态数组来做到这一点.分析它以使您的代码成为您需要的(图像,文本)

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

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

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

第 2 步: 创建新的 UITableViewCell(newcell) cocoa touch 类文件.

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

第 3 步: 然后转到 identity->custom class -> 将创建的 UITableViewCell 可可触摸类文件添加到您的原型单元格(单元格).然后将 UILabel 添加到您的原型单元格中.

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

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

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

第 5 步: 将新的 UIViewController 添加到您的故事板以用于 collectionView 并遵循与 UITableView

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

第 6 步:创建从视图控制器到集合视图控制器的推送 Segue 并命名该 Segue.

Step 6: Create push Segue from viewcontroller to collection viewcontroller and name the 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 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
}
}

这篇关于将表视图(行选择)的数据传递给视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆