I am trying to create a segue so every time I click a tableView row, show another ViewController with the information of the specific row.
I populate my data from Firestore.
Basically every document contains an array and then I populate the strings of the array in rows
var ingredientsArray = [Ingredients]()
func numberOfSections(in tableView: UITableView) -> Int {
return ingredientsArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ingredientsArray[section].compName.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "SearchDetails", sender: self)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "IngredientCell", for: indexPath) as! IngredientTableViewCell
cell.populate(ingredient: ingredientsArray[indexPath.section])
let item1 = ingredientsArray[indexPath.section].compName[indexPath.row]
cell.ingredientNameLabel.text = ("\(item1)")
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailViewController{
//HERE IS THE ERROR.
destination.ingredient = ingredientsArray[(tableView.indexPathForSelectedRow?.row)!]
}
}
When I click some rows then my app crashes and gives me Fatal error: Index out of range
DetailViewController
class DetailViewController: UIViewController {
#IBOutlet weak var compNameLbl: UILabel!
var ingredient : Ingredients?
override func viewDidLoad() {
super.viewDidLoad()
compNameLbl.text = "\((ingredient?.compName)!)"
}
}
Also when I try to show the name in the label the whole array appears.
Get the string value from the compName array and pass the value
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailViewController, let indexPath = tableView.indexPathForSelectedRow {
destination.ingredient = ingredientsArray[indexPath.section].compName[indexPath.row]
}
}
Change the ingredient type to String in DetailViewController
class DetailViewController: UIViewController {
#IBOutlet weak var compNameLbl: UILabel!
var ingredient : String?
override func viewDidLoad() {
super.viewDidLoad()
compNameLbl.text = ingredient
}
}
Here I am sending secondVC textfield value to firstVC tableview but I am not receiving delegate value in tableview.
Here is my code:
In firstVC:
import UIKit
class CreateBusinessViewController: UIViewController, MyDataSendingDelegateProtocol, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var iteamsArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
func sendDataToFirstViewController(myData: String) {
self.iteamsArray.append(myData)
print(iteamsArray)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "addSegue") {
let vc = segue.destination as! CreatePopUpViewController
vc.delegate = self
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return iteamsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BusinessTableViewCell
cell.nameLabel.text = iteamsArray[indexPath.row]
return cell
}
}
In secondVC:
protocol MyDataSendingDelegateProtocol {
func sendDataToFirstViewController(myData: String)
}
class CreatePopUpViewController: UIViewController {
var delegate: MyDataSendingDelegateProtocol?
#IBOutlet weak var addTf: UITextField!
#IBAction func saveButn(_ sender: Any) {
self.delegate?.sendDataToFirstViewController(myData: addTf.text!)
dismiss(animated: true, completion: nil)
}
}
How to add secondVC textfield data to firstVC array?
To Reflect the data to tableview cell you just need to reload the table view like i have updated your function check below code.
func sendDataToFirstViewController(myData: String) {
self.iteamsArray.append(myData)
tableView.reloadData()
print(iteamsArray)
}
Hope this way may help you.
I am trying to finish this app that is a form. once filled out a button later will be pushed for print. The first controller has a tableview that has page 1 page 2 page 3. page 1 opens and you fill in all info. when hit save it should take you back to first controller. then when you push print opens the tableview and loads all info.
I am struggling to use structs correctly. also struggling on the save button to the delegate to the print controller page.
my page controller code
class PagesController: UIViewController, UITableViewDelegate {
var pages = ["Page 1","Page 2","Page 3"]
#IBOutlet weak var pagesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
pagesTableView.delegate = self
pagesTableView.dataSource = self
}
#IBAction func printBtnPressed(_ sender: Any) {
} }
extension PagesController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PagesCell")
cell?.textLabel?.text = pages[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "ChildInfoSegue", sender: self)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 90.0
}
}
Heres my form controller
protocol ChildDelegate {
func saveInfoBlock(form: ChildInfo)
}
class ChildInfoTableViewController: UITableViewController {
var formDelegate: ChildDelegate!
var firstNameText: UITextField = UITextField()
var lastNameText: UITextField = UITextField()
var middleNameText: UITextField = UITextField()
//MARK: From ChildInfo.swift STRUCT
var childInfo = [ChildInfo]()
var basicChildInfo = ["Child's First Name","Child's Middle Name","Child's Last Name"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return basicChildInfo.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChildInfoCell") as! ChildInfoTableViewCell
cell.formLabel.text = basicChildInfo[indexPath.row]
return cell
}
//MARK: Segue to printController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let path = self.tableView.indexPathForSelectedRow!
let destViewController = segue.destination as! PrintPageTableViewController
}
//MARK: Cancel Button Pressed
#IBAction func cancelBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
#IBAction func saveBtnPressed(_ sender: Any) {
let firstname = firstNameText.text ?? ""
let middlename = middleNameText.text ?? ""
let lastname = lastNameText.text ?? ""
let formData = ChildInfo(cFirstName: firstname, cMiddleName: middlename, cLastName: lastname)
self.navigationController?.popViewController(animated: true)
formDelegate.saveInfoBlock(form: formData)
saveInfoBLock()
}
func saveInfoBLock() {
print("Saving Info")
dismiss(animated: true, completion: nil)
}
}
Heres my struct that I don't properly use
struct ChildInfo {
var childFirstName: String?
var childMiddleName: String?
var childLastName: String?
var childsMadeUpName: String = ""
init(cFirstName: String, cMiddleName: String, cLastName: String) {
self.childFirstName = cFirstName
self.childMiddleName = cMiddleName
self.childLastName = cLastName
}
}
and lastly my tableviewcell
class ChildInfoTableViewCell: UITableViewCell {
#IBOutlet weak var formText: UITextField!
#IBOutlet weak var formLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Once the delegate protocol gets passed. What would a put in the printtableviewcontroller to make the data show? I had someone help me on here, but It was all programmatically and I'm no that advanced yet.
There are several things you can do:
Use delegation. I am assuming this is what you want to achieve here.
Pass your data through Segues. ( Recommended )
To use delegation, you have to have a clear concept of how delegation works.
Here is a link to an explanation I gave someone on another site. It breaks it down, I think this may help you a bit.
https://teamtreehouse.com/community/totally-confused-with-the-example-used
For now, I think the solution to your problem is to use Segues.
When you tap on the button to take you to the ChildInfoTableViewController where you fill the information is where you want to create your "ChildInfo" object. Then when you press save, you trigger the segue and pass the "ChildInfo" object to the "PagesController".
Then from there, that same object should be pass through the segue into the print view.
What I want to achieve: I want to click on one of the rows in my first view controller to move to another table view controller.
The problem: When I click the row, the view only shows "Label" and not the data I intend to pass through. Granted the application does not crash and the white background with the "Label" heading shows up, the data is still not being shown on the 2nd view controller.
What I have done so ffar: I have used a Struct: PageTwoItems to define the data I want to send to the 2nd View Controller.
import Foundation
import UIKit
The code for the second view controller is as follows:
class PageTwoTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var PageTwo = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PageTwo.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = self.tableView.dequeueReusableCellWithIdentifier("secondcell", forIndexPath: indexPath) as UITableViewCell
Cell.textLabel?.text = PageTwo[indexPath.row]
return Cell
}
}
The code for the first view controller is as follows:
#IBOutlet weak var tableView: UITableView!
let names = ["Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive"]
let desc = ["Get your spotless home or office space", "Drains, Pipes, Faucets and more", "Lighting, Fans, AC's and more", "Installation, Assembly and more", "Tow Truck Hire, Tyre Replacement and more"]
let images = [UIImage(named:"pug"),UIImage(named:"pug2"),UIImage(named:"pug3"),UIImage(named:"pug4"),UIImage(named:"pug5")]
var PageTwo = [PageTwoItems]()
override func viewDidLoad() {
PageTwo = [PageTwoItems(nametwo:["Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning"], summarytwo:["Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered"],phototwo:["","",""]),
PageTwoItems(nametwo:["Drains, Pipes & Faucets", "Showers and Bath Tubs", "Toilet and Wash Basin", "Water Heater"], summarytwo:["Fix Your Broken Pipes, Clogged Drains and Leaky Faucets","Showers and Bath Tubs working just right"," ", " "],phototwo:["","",""]),
PageTwoItems(nametwo:["Lighting Fixtures", "Air Conditioners & Fans", "Generators"], summarytwo:["..","..",".."],phototwo:["","",""]),
PageTwoItems(nametwo:["Furniture Assembly/Installation", "Interior Painting", "Doors, Windows & Curtains"], summarytwo:["..","...","..."],phototwo:["","",""]),
PageTwoItems(nametwo:["Tow Truck Hire", "Tyre/Vulcanizer Help", "Auto-Consultant"], summarytwo:["...","...","..."],phototwo:["","",""])]
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("newcell", forIndexPath:indexPath) as!CustomCell
cell.photo.image = images[indexPath.row]
cell.summary.text = desc[indexPath.row]
cell.name.text = names[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let DestViewController = segue.destinationViewController as! PageTwoTableViewController
var PageTwoArrayTwo : PageTwoItems
PageTwoArrayTwo = PageTwo[indexPath.row]
DestViewController.PageTwo = PageTwoArrayTwo.nametwo
DestViewController.PageTwo = PageTwoArrayTwo.summarytwo
DestViewController.PageTwo = PageTwoArrayTwo.phototwo
}
}
The struct for the Page Two Items:
struct PageTwoItems {
var nametwo : [String]
var summarytwo : [String]
var phototwo : [String]
}
I think your data isn't organized correctly. What I think you are looking for is something like this:
Cleaning
Home cleaning
Office cleaning
Etc
Plumbing
Drains, Pipes & Faucets
Showers and Bath Tubs
Etc
Electrical
Lighting Fixtures
Air Conditioners & Fans
Generators
This is how I would create data structures to support this.
// This represents a single row on the detail screen
struct PageTwoItem {
var name : String
var summary : String
var photo : String
}
// this represents a single row on the main screen
struct PageData {
var name: String // goes into the table on main screen
var subitems: [PageTwoItem] // send this to the detail
}
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
// Make an array of PageData
var objects = [PageData]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupData()
}
func setupData() {
objects = [
PageData(name: "Cleaning", subitems: [
PageTwoItem(name: "Cleaning 1", summary: "", photo: ""),
PageTwoItem(name: "Cleaning 2", summary: "", photo: ""),
PageTwoItem(name: "Cleaning 3", summary: "", photo: ""),
PageTwoItem(name: "Cleaning 4", summary: "", photo: "")
] ),
PageData(name: "Plumbing", subitems: [] ),
PageData(name: "Electrical", subitems: [] ),
PageData(name: "Craftswork", subitems: [] ),
PageData(name: "Automotive", subitems: [] ),
]
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print(segue.identifier)
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = objects[indexPath.row]
let controller = segue.destinationViewController as! DetailViewController
// tell the detail controller what we want her to show
controller.detailItem = object
}
}
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let object = objects[indexPath.row]
cell.textLabel!.text = object.name
return cell
}
}
In the detail controller :-
import UIKit
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
// here is where we put the data we want to show
var detailItem: PageData?
// MARK: - Table View
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let detailItem = detailItem {
print("count = \(detailItem.subitems.count)")
return detailItem.subitems.count
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DetailCell", forIndexPath: indexPath)
if let object = detailItem?.subitems[indexPath.row] {
cell.textLabel!.text = object.name
}
return cell
}
}
I think the segue you use is directly connected to tableview cell to your second view controller.
You should connect your segue between your 2 controllers and set an identifier to the attributes inspector like showSecondControllerSegue and use this method :
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier(identifier: "showSecondControllerSegue", sender: self)
}
Edit :-
Your code should be :
For the second view controller :
class PageTwoTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var pageTwo = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pageTwo.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = self.tableView.dequeueReusableCellWithIdentifier("secondcell", forIndexPath: indexPath) as UITableViewCell
Cell.textLabel?.text = pageTwo[indexPath.row]
return Cell
}
}
For the first view controller :
#IBOutlet weak var tableView: UITableView!
let names = ["Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive"]
let desc = ["Get your spotless home or office space", "Drains, Pipes, Faucets and more", "Lighting, Fans, AC's and more", "Installation, Assembly and more", "Tow Truck Hire, Tyre Replacement and more"]
let images = [UIImage(named:"pug"),UIImage(named:"pug2"),UIImage(named:"pug3"),UIImage(named:"pug4"),UIImage(named:"pug5")]
var pageTwo = [PageTwoItems]()
override func viewDidLoad() {
super.viewDidLoad()
pageTwo = [PageTwoItems(nametwo:["Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning"], summarytwo:["Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered"],phototwo:["","",""]),
PageTwoItems(nametwo:["Drains, Pipes & Faucets", "Showers and Bath Tubs", "Toilet and Wash Basin", "Water Heater"], summarytwo:["Fix Your Broken Pipes, Clogged Drains and Leaky Faucets","Showers and Bath Tubs working just right"," ", " "],phototwo:["","",""]),
PageTwoItems(nametwo:["Lighting Fixtures", "Air Conditioners & Fans", "Generators"], summarytwo:["..","..",".."],phototwo:["","",""]),
PageTwoItems(nametwo:["Furniture Assembly/Installation", "Interior Painting", "Doors, Windows & Curtains"], summarytwo:["..","...","..."],phototwo:["","",""]),
PageTwoItems(nametwo:["Tow Truck Hire", "Tyre/Vulcanizer Help", "Auto-Consultant"], summarytwo:["...","...","..."],phototwo:["","",""])]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("newcell", forIndexPath:indexPath) as!CustomCell
cell.photo.image = images[indexPath.row]
cell.summary.text = desc[indexPath.row]
cell.name.text = names[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier(identifier: "showSecondControllerSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let DestViewController = segue.destinationViewController as! PageTwoTableViewController
var pageTwoArrayTwo : PageTwoItems
pageTwoArrayTwo = PageTwo[indexPath.row]
DestViewController.pageTwo = pageTwoArrayTwo.nametwo
DestViewController.pageTwo = pageTwoArrayTwo.summarytwo
DestViewController.pageTwo = pageTwoArrayTwo.phototwo
}
I have an App that i'm trying to adapt exactly how i want
I have been following a Youtube tutorial of Seemu Apps to make it but I need to finish it adding an optional ViewController
This app has 2 tableViews showing vehicles and if we click in one row of the first tableView then second tableView will show us a list of selected vehicles.
Here is what we have until now: (image link , because i haven't got ten points reputation on stackOverFlow)
http://subefotos.com/ver/?65ba467040cb9280e8ec49644fd156afo.jpg
All is running perfect, but i want to be able to display information in an optional detailViewController (label with a detailed description of each vehicle and a bigger image of this ) depending of which vehicle we click in the secondTableViewControlle (or modelViewController in the App) exactly how i was following in the tutorial between tableViews
i know that we need to passing data through prepareForSegue method , i have understood this making the steps in the tutorial but when we have 2 tableviewControllers
For example : if we want to display a last viewController with information of Ferrari 458 and a great picture of this car
What do we need to do exactly to show information of each vehicle?
PD : I'm beginner in the programming world, maybe i would need to see it in a very simple way
The whole code:
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var selMake = String()
#IBOutlet var tableView : UITableView!
var transportData : [String] = ["Car", "Plane", "Motorcycle", "Truck" , "Train", "Bicycle" , "Helicopter"]
//////////////////////////////////////////
//viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
//Register custom cell
var nib = UINib(nibName: "customCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
//Numbers of rows in Section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.transportData.count
}
//cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
///// Static Cell (no valid for custom cells)
/*
var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.transportData[indexPath.row]
return cell
*/
var cell:customCellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as customCellTableViewCell
cell.lblTrans.text = transportData[indexPath.row]
cell.imgTrans.image = UIImage (named: transportData[indexPath.row])
return cell
}
//height
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 90
}
//didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Fila \(transportData[indexPath.row]) seleccionada")
selMake = transportData[indexPath.row]
performSegueWithIdentifier("modelView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "modelView") {
var vc = segue.destinationViewController as modelViewViewController
vc.selMake = selMake
}
}
import UIKit
class customCellTableViewCell: UITableViewCell {
#IBOutlet weak var imgTrans: UIImageView!
#IBOutlet weak var lblTrans: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class modelViewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//////////////////////////////////
var selMake = String()
var tableData : [String] = []
#IBOutlet var tableView: UITableView!
//////////////////////////////////
override func viewDidLoad() {
super.viewDidLoad()
//Register custom cell
var nib = UINib(nibName: "customCell2", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
switch selMake {
case "Car" :
tableData = ["Ferrari 458", "La Ferrari"]
case "Plane" :
tableData = ["Iberia"]
case "Motorcycle" :
tableData = ["Kawasaki Ninja", "Yamaha Aerox"]
case "Truck" :
tableData = [ "Camion transporte"]
case "Train" :
tableData = [ "Ave" ]
case "Bicycle" :
tableData = ["BMX"]
case "Helicopter" :
tableData = ["HelicopteroCombate"]
default:
println("Sel Make \(selMake)")
}
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
/* var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.tableData[indexPath.row]
return cell*/
var cell:customCell2TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as customCell2TableViewCell
cell.lbl2text.text = self.tableData[indexPath.row]
cell.img2image.image = UIImage (named: tableData[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Row \(indexPath.row)selected")
performSegueWithIdentifier("detailView", sender: self)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 90
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
var vc = segue.destinationViewController as DetailViewController
}
}
import UIKit
class customCell2TableViewCell: UITableViewCell {
#IBOutlet var lbl2text: UILabel!
#IBOutlet var img2image: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class DetailViewController: UIViewController {
#IBOutlet var imgDetail: UIImageView!
#IBOutlet var lblDetail: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
Try this.
ModelViewViewController
var selectedImage:String?
var selectedLabel:String?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Row \(indexPath.row)selected")
selectedImage = self.tableData[indexPath.row]
selectedLabel = self.tableData[indexPath.row]
performSegueWithIdentifier("detailView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
var vc = segue.destinationViewController as DetailViewController
vc.img = selectedImage
vc.lblDetail = selectedLabel
}
}
class DetailViewController: UIViewController {
#IBOutlet var imgDetail: UIImage!
#IBOutlet var lblDetail: UILabel!
var img:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgDetail = UIImage(named: img)
}
This should work.