JuanCa2607 JuanCa2607 - 26 days ago 22
Swift Question

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

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

Answer

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