I'm trying to do maybe one of the simplest and more confusing things for me until now
I wanna develop my own App , and in order to do it I need to be able to passing some information depending of which row user click (it's Swift lenguage)
We have a RootViewController(table view) and a DetailViewController (with 1 label and 1 image)
(our view)
Here is the code:
#IBOutlet weak var tableView: UITableView!
var vehicleData : [String] = ["Ferrari 458" , "Lamborghini Murcielago" , "Bugatti Veyron", "Mercedes Benz Biome"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var nib = UINib(nibName: "TableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return vehicleData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TableViewCell
cell.lblCarName.text = vehicleData[indexPath.row]
cell.imgCar.image = UIImage(named: vehicleData[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("DetailView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "DetailView") {
var vc = segue.destinationViewController as DetailViewController
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
Custom TableViewCell class (has a xib File with cell)
class TableViewCell: UITableViewCell {
#IBOutlet weak var lblCarName: UILabel!
#IBOutlet weak var imgCar: 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
}
class DetailViewController: UIViewController {
#IBOutlet weak var lblDetail: UILabel!
#IBOutlet weak var imgDetail: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
The question is:
if user click Ferrari 458 , the lblDetail in DetailViewController would show: Ferrari 458 is a super car which is able to reach 325 km/ h ...... (whatever we want)
and imgDetail would be able to show an image (whatever we want) of the car
If user click Bugatti Veyron now the lblDetail show us: Bugatti Veyron is a perfect and super sport machine. It's one of the fastest car in the world....
imgDetail show us an image of this car
Same thing with all cars depending which row we have clicked
I know the work is around prepareForSegue func in first View Controller but i was trying a lot of different ways to make it possible and anything runs ok
How we can do this???
Here is the example for you:
var valueToPass:String!
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
valueToPass = currentCell.textLabel.text
performSegueWithIdentifier("yourSegueIdentifer", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "yourSegueIdentifer") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as AnotherViewController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
}
But don't forget to create a passedValue variable into your DetailViewController.
This is just an example of passing data from one viewController to another and you can pass data with this example as you need.
And for more info refer this links.
Passing values between ViewControllers based on list selection in Swift
Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?
Swift: Pass UITableViewCell label to new ViewController
https://teamtreehouse.com/forum/help-swift-segue-with-variables-is-not-working
May be this will help you.
Swift 3.0
var valueToPass:String!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
valueToPass = currentCell.textLabel?.text
performSegue(withIdentifier: "yourSegueIdentifer", sender: self)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "yourSegueIdentifer") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destination as! AnotherViewController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
}
This may be another solution, without much code in didSelectRowAtIndexPath method.
Note that while it may look cleaner, and we do not need an extra variable valueToPass, it may not be a best practice, because the sender argument inside performSegue method is supposed to be the actual object that initiated the segue (or nil).
// MARK: UITableViewDelegate methods
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "goToSecondVC", sender: indexPath)
}
// MARK: UIViewController methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToSecondVC" {
if segue.destination.isKind(of: CarDetailsController.self) {
let secondVC = segue.destination as! CarDetailsController
let indexPath = sender as! IndexPath
secondVC.passedValue = carsArray[indexPath.row]
}
}
}
If you drag a segue from the prototype cell (in the Interface Builder) to your next View Controller and set its segue identifier to "Your Segue Identifier", you can also do it with this shortcut:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Your Segue Identifier" {
let cell = sender as! YourCustomCell
let vc = segue.destination as! PushedViewController
vc.valueToPass = cell.textLabel?.text // or custom label
}
}
And you also don't need the performSegueWithIdentifier() in the didSelectRowAtIndexPath(), nor this Table View method.
In PushedViewController.swift (the next View Controller):
var valueToPass: String!
override func viewDidLoad() {
super.viewDidLoad()
yourLabel.text = valueToPass
}
It's important to set the label's value after it initialized from the Storyboard. That means, you can't set the label in the previous View Controller's prepareForSegue() directly, therefore needing to pass it with valueToPass.
Its simple, am adding one statement to above answer.
To get the selected car name in detail view label,
lblDetail.text = passedValue
you can add this code of line in viewDidLoad() func of your detailed view. passedValue contains the name of car which user selected(assign in prepareForSegue) then you can assign to your detailedView label.
Hope it helps!!
Related
VC-A is embedded in a nav controller and has a button going to VC-B via a popover segue. VC-B has a table view with a few font names. When I select a font name, VC-B closes and, using delegate/protocol, VC-A should get the selected name. It does not. I found that if I set a breakpoint at the end of didSelectRowAt, delegate is nil for some reason.
VC-A
class ViewController: UIViewController, FontDelegate {
#IBOutlet weak var infoLabel: UILabel!
let fontTable = FontTableTableViewController()
override func viewDidLoad() {
super.viewDidLoad()
fontTable.delegate = self
}
func getFontName(data: String) {
infoLabel.text = data
}
}
VC-B
protocol FontDelegate {
func getFontName(data: String)
}
class FontTableTableViewController: UITableViewController {
let fontArray = ["Helvetica", "Arial", "Monaco"]
var delegate: FontDelegate?
// MARK: - Table view functions go here...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let font = fontArray[indexPath.row]
self.delegate?.getFontName(data: font)
self.dismiss(animated: true, completion: nil)
}
}
This shows the storyboard connection from the button in VC-A to VC-B.
Instead of linking Pick your font button to the other controller, I would suggest you creating an IBAction of the button to trigger the following code whenever pressed:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
guard let vc = storyBoard.instantiateViewController(withIdentifier: "FontScreen") as? FontTableTableViewController else {return}
vc.delegate = self
vc.modalPresentationStyle = .fullScreen
self.navigationController?.pushViewController(vc, animated: true)
but for this to work you will need to go to the identity inspector of your FontTableTableViewController and from there you can name your controller in storyboardID field in Identity section as FontScreen.
This is not the way how to return a variable in a navigation controller Environment. I would like to suggest you use an unwind segue. Your code would then look like:
VC A
class ViewController: UIViewController {
var selectedFontName = ""
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func returnSelectedFont(sender: UIStoryboardSegue) {
print(selectedFontName)
}
}
The IBAction in VC A ist the method too which the tableview returns to if you use an unwind segue.
Your tableview controller then looks like this:
VC B
import UIKit
class FontTableTableViewController: UITableViewController {
var selectedFont = ""
let fontArray = ["Helvetica", "Arial", "Monaco"]
#IBOutlet var fontTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
fontTable.dataSource = self
fontTable.delegate = self
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return fontArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FontTable", for: indexPath) as! FontCell
cell.fontName.text = fontArray[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedFont = fontArray[indexPath.row]
performSegue(withIdentifier: "Return Fontname", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let returnVC = segue.destination as? ViewController {
returnVC.selectedFontName = selectedFont
}
}
}
To be able to get the return segue working you have to select the tableview cell and control drag it to the exit icon in the tableview controller. See therefore the picture.
After that you select the unwind segue and give it an identifier, to be used in VC B. See the code:
This should work perfectly.
I have a table view populated through a Nib.
Below is the .xib
And the .swift
import UIKit
class PeopleCell: UITableViewCell {
#IBOutlet weak var peopleCellLabel: UILabel!
#IBOutlet weak var labelTest: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
#IBAction func button(_ sender: UIButton) {
labelTest.text = "Y"
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
In my PeopleDetailsController, how can I retrieve label.text ?
Below code only return "Label" (which is the default value) even if the button was pressed.
extension PeopleDetailsController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifierPeople, for: indexPath) as! PeopleCell
print(cell.labelTest.text)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
}
If you were to do this, you’d retrieve the cell via cellForRow(at:) (not to be confused with the delegate method tableView(_:cellForRowAt:)) which returns the cell associated with an indexPath (assuming that cell is visible):
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRowAt(at: indexPath) as? PeopleCell else {
return
}
print(cell.labelTest.text)
}
But this pattern suggests that you’re storing model data within a view, which can be problematic. What if the row scrolled out of view and was discarded or reused? If you scroll it back into view, your string value has been lost.
Your button action handler should trigger the update of the model. Then didSelectRowAt would look up the value in the model, not in the cell.
I want send data between UITableViewCell and UIViewController
I have a list of address, in my cell I have a button (edit) and I want to send all the information about the address in the cell
#IBOutlet weak var address: UILabel!
#IBOutlet weak var rfc: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is ContainerVC{
let vc = segue.destination as? ContainerVC
vc.address = self.address.text
}
}
Method does not override any method from its superclass
Let's use segue inside your controller which contains the tableview which uses your UITableViewCell
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueIdentifier,
let destination = segue.destination as? ContainerVC,
let selectedIndex = tableView.indexPathForSelectedRow?.row
{
destination.address = self.addresses[selectedIndex].text
self.navigationController?.pushViewController(destination, animated: true) // For example
}
}
To pass data from cell Class to Controller Class use delegate methods on edit action.
Define call back delegate method in UITableViewCell class:
var callBack: ((NSDictionary) -> (Void))?
Use DS as per your need.
On Edit Action call delegate method and pass the data
#IBAction func editAction(_ sender: Any) {
callBack?(dictionary)
}
In cellForRowAt indexPath call delegate method with cell object
cell.callBack = { (currentData : NSDictionary)
in
// Perform Action on data
}
To Pass Data from Controller class to cell class you can simply define a method in cell class and can call in cellForRowAt indexPath.
Define SetData(dictionary) in cell class
In View Controller
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "CellName", for: indexPath) as! CellClassName
cell.SetData(dict)
return cell
}
You need to do this from your UIViewController in which the UITableViewCell is implemented:
Assuming you have an array to populate your UITableView, the code would be something like:
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var addresses = [String]()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAddressDetail" {
if let secondViewController = segue.destination as? SecondViewController {
secondViewController.address = sender as! String
}
}
}
}
extension FirstViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.addresses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: YourTableViewCell = tableView.dequeueReusableCell(withIdentifier: "YourTableViewCell", for: indexPath) as! YourTableViewCell
cell.address = self.addresses[indexPath.row]
cell.delegate = self
return cell
}
Since you have to call your function from an action triggered by a button on your UITableViewCell, you can implement a protocol on your cell:
import UIKit
protocol YourTableViewCellDelegate: class {
func selectedAddress(address: String)
}
class YourTableViewCellDelegate: UITableViewCell {
weak var delegate: YourTableViewCellDelegate?
var address: String
}
And in your button action, call your delegate method like this:
#IBAction func selectAddress() {
self.delegate?.selectedAddress(address: self.address)
}
This will trigger the delegate on your UIViewController. To handle the call, don't forget to assign your cell delegate to your view controller and implement your cell delegate in your controller:
extension FirstViewController: YourTableViewCellDelegate {
func selectedAddress(address: String) {
// Do stuff with the selected address
}
}
I have an app that starts with tab bar with 3 items,2 of them is just a regular view controller and one of them is navigation controller and after him is table view,when I click on a cell I want to pass 2 String to the labels and to pass to a new view controller to show the label's,my problem is that after I click on the cell its looks like its jump 2 view controller's until the view controller that I want, and in the first time I didn't see my data that I pass, only after I press to come back to the table view controller(here I need to back to him from 2 view controllers), please help me to fix my code, I want to pass the data and move to a new view controller and when I want to come back to the table view I need to click just back
//Table view controller, before of him I have a navigation controller
//The identifier of the segue is "Details"
import UIKit
class HistoryTableViewController:
UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var historyTableView: UITableView!
var historyArray = [history]()
var foods = ["Milk","Honey","Salt","Bread","Banana"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foods.count
//historyArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = historyTableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryCellViewController
else {return UITableViewCell()}
// cell.ageLabel.text = String(format:"%f", historyArray[indexPath.row].age)
// cell.genderLabel.text = historyArray[indexPath.row].gender
// cell.ageLabel.text = String(format:"%f", 23)
cell.genderLabel.text = foods[indexPath.row]
cell.ageLabel.text = foods[indexPath.row]
// cell.genderLabel.text = "Bar"
//cell.historyImage.image = nil
return cell
}
var selectedAge:String = " "
var selectedGender:String = " "
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedAge = self.foods[indexPath.row]
selectedGender = self.foods[indexPath.row]
performSegue(withIdentifier: "Details", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Details"{
let vc = segue.destination as! HistoryDetailsViewController
vc.age = selectedAge
vc.gender = selectedGender
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
foods.remove(at: indexPath.row)
historyTableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
//view controller that I want to pass after a click on a cell
import UIKit
class HistoryDetailsViewController: UIViewController {
#IBOutlet weak var ageLabel: UILabel!
#IBOutlet weak var genderLabel: UILabel!
var age:String?
var gender:String?
override func viewDidLoad() {
super.viewDidLoad()
ageLabel.text = self.age
genderLabel.text = self.gender
// Do any additional setup after loading the view.
}
//
// #IBAction func Back(_ sender: UIBarButtonItem) {
//
// // self.tabBarController?.selectedIndex = 0
// }
}
First of all, in tableView(_:cellForRowAt:) you're typecasting the dequeued cell to HistoryCellViewController.
My question is - what is the type of HistoryCellViewController? Is it a UITableViewCell or something else? In case it is a UITableViewCell you must rename it to something more relevant like HistoryCell. It is a bit confusing.
Approach-1:
Now coming to the actual problem you're facing. Instead of using the segue, try pushing the HistoryDetailsViewController manually like so,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let controller = self.storyboard?.instantiateViewController(withIdentifier: "HistoryDetailsViewController") as? HistoryDetailsViewController {
controller.age = self.foods[indexPath.row]
controller.gender = self.foods[indexPath.row]
self.navigationController?.pushViewController(controller, animated: true)
}
}
There is no need to extra variables - selectedAge and selectedGender.
Approach-2:
In case you want to do it with the segue itself, then don't call performSegue(withIdentifier:sender:) in tableView(_:didSelectRowAt:), i.e. remove the below line of code,
performSegue(withIdentifier: "Details", sender: self)
This is because, the "Details" segue is being executed twice - once via storyboard and another via the above line of code.
I've UITableView and I'm passing selected Item from one UIViewController to another. To achieve this I have array of Item objects.
var array = [Item]()
for performing segue I'm passing sender parameter of buttonPressed action as sender of performSegue method.
#IBAction func buttonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "identifier", sender: sender)
}
This button is inside UITableViewCell and has tag equal to indexPath.row which I set in TableView cellForRowAt data source method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
cell.myButton.tag = indexPath.row
...
}
Then in ViewController's prepare method I downcast sender as UIButton and then I assign selectedItem variable in destination ViewController as Item from array with button.tag as index.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier" {
let button = sender as! UIButton
let destinationVC = segue.destination as! ViewController2
destinationVC.selectedItem = array[button.tag]
}
}
Q: This works just fine, I'm using this for a long time but I have got feeling that this isn't the right solution. Is there any better?
My preference is to use the delegate pattern here.
In this approach, the view controller makes itself the delegate of every tableview cell it creates. It passes the Item to the cell. When the button in the cell is tapped, the cell sends the Item back to the view controller. In this way the view controller knows which Item was selected and should be sent to the next view controller.
To implement, begin by declaring a variable in your tableview cell to hold the Item:
weak var item: Item?
In your view controller, pass the item to the cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//...
cell.item = array[indexPath.row]
//...
}
Next, declare a delegate protocol:
protocol ItemSelectionDelegate {
func itemSelected(_ item: Item)
}
Add a variable of this type to your custom table view cell, like so:
weak var delegate: ItemSelectionDelegate?
Make the button in your tableview cell call the delegate when tapped, passing it the item:
#IBAction func buttonPressed(_ sender: UIButton) {
if let item = item {
delegate?.itemSelected(item)
}
}
Now, make your view controller conform to this protocol:
class MyViewController: UIViewController, ..., ItemSelectionDelegate {
func itemSelected(_ item: Item) {
//...
}
}
Make sure to set the view controller as the delegate of each cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//...
cell.item = array[indexPath.row]
cell.delegate = self
//...
}
Call your segue, but pass the Item instead of a button:
class MyViewController: UIViewController, ..., ItemSelectionDelegate {
func itemSelected(_ item: Item) {
performSegue(withIdentifier: "identifier", sender: item)
}
}
Now pass this to your next view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier" {
let destinationVC = segue.destination as! ViewController2
destinationVC.selectedItem = sender as? Item
}
}
This is definitely more work to set up, so use only if it makes sense to you.
If I don't get wrong, you had to get selected index path row in prepareForSegue:
if segue.identifier == "identifier" {
guard let destinationVC = segue.destination as? ViewController2 else { return }
guard let selectedIndexPath = self.tableView.indexPathForSelectedRow else { return }
destinationVC.selectedItem = selectedIndexPath.row
}