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

谁都会走 提交于 2019-12-08 08:43: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.

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


回答1:


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 UITableView to you ViewController. Then add one prototype cell and name it(eg.cell).

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

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.

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

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

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

}
}

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 = ["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
}
}



回答2:


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



来源:https://stackoverflow.com/questions/38779724/passing-data-of-table-view-row-select-to-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!