ios - 将 TableView (行选择)的数据传递给 View Controller

标签 ios swift xcode uitableview viewcontroller

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

我使用协议(protocol)和这个:

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

谢谢

enter image description here

最佳答案

好吧,我将展示如何使用静态字符串数组来实现这一点。对其进行分析,使您的代码符合您的需要(图片、文本)

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

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

第 3 步: 然后转到 identity->custom class -> 将创建的 UITableViewCell cocoa touch 类文件添加到您的原型(prototype)单元格(cell) .然后将 UILabel 添加到您的原型(prototype)单元格。

第 4 步:创建 TableView 委托(delegate)以查看 Controller 并为标签创建导出到 newcell,然后创建 TableView 到 View Controller 。

第 5 步:将新的 UIViewController 添加到 collectionView 的 Storyboard中,并遵循与 UITableView

类似的方法

第 6 步:创建从 viewcontroller 到集合 viewcontroller 的推送 Segue 并命名 segue。

之后,将以下代码添加到具有 TableView 的 View Controller 中。

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

}
}

将以下代码添加到您的 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 = ["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
}
}

关于ios - 将 TableView (行选择)的数据传递给 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38779724/

相关文章:

ios - 在我的游戏中添加一个 SKEmitter 节点

javascript - 文本链接到 <a href ='...' >//仅 JavaScript

xcode - Swift 中核心数据的自动重新排序

swift - XC框架 "Cannot load underlying module"

ios - 找不到 'refs/heads/master'引用错误

cocoa-touch - 更改键盘语言

ios - "Yo"ios 应用程序如何知道用户何时禁用推送通知?

ios - 键盘在 iOS 中运行非常缓慢

json - 用 Codable 解码这种 JSON

swift 、Firebase : `setValue` giving error "AnyObject cannot be used with Dictionary Literal"