Swift: UICollectionView selecting cell indexPath issues - swift

I am trying to do a Collection View whereby someone selects a cell and for each selection it takes them to another View Controller that holds content of the selection. However I'm running into difficulties as once I apply this line of code to didSelectItemAtIndexPath;
self.performSegueWithIdentifer("showDetail", sender: self)
and then run it in the Simulator the cell selection is working according the indexPath but its remembering the selections each time I select new cell. So for example each cell has a photo and label and if I select the first cell in the indexPath the segue takes me first to blank view and then to my selected cell. If I select another cell, number 3 on the indexPath the blank view is now the first cell from my previous choice after which it takes to my selected third cell . Its doing that every time. If I remove the performSegueWithIdentifer code (from Xcode 6.2 (in 6.1.1 it was random)) the selection is my previous choice and never my 'selectedCell', but then at least its only selecting once instead of twice to get to a view. There is something going wrong on the indexPath. This is the code for my prepareForSegue
override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
if segue.identifer == "showDetail" {
let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
detailVC.selectedImageName = selectedImage
detailVC.selectedLabel = selectedLabels
}
}
I'm stuck on what to do & what solution to apply. Do I keep performSegueWithIdentifer code & create an Equatable to implement find(array, selection) on the indexPath? Or could I write a loop, (which seems much easier), that would run through the indexPath based upon the selections and that would remove the cell that is no longer selected. However I'm not sure what condition to write in the loop because I don't know the value of the property of the 'selectedCell' because its optional.
for (index, value) in enumerate(cellItems) {
//something here to remove 'selectedItem' in the indexPath
}
If I remove performSegueWithIdentifer code from didSelectItemAtIndexPath what can I do in my prepareForSegue to get the selection on the correct indexPath?
EDIT the complete code at didSelectItemAtIndexPath
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedImage = cellImages[indexPath.row] as String
selectedLabels = cellLabels[indexPath.row] as String
self.performSegueWithIdentifer("showDetail", sender: self)
}
I've tried changing sender in the performSegueWithIdentifer to indexPath but the problem still remains.
EDIT 2 Complete code to my CollectionViewController
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet weak var collectionView: UICollectionView!
var selectedImage = String()
var selectedLabels = String()
var cellImages:[String] = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "13.jpg", "14jpg"]
var cellLabels:[String] = ["Photo 1", "Photo 2", "Photo 3", "Photo 4", "Photo 5", "Photo 6", "Photo 7", "Photo 8", "Photo 9", "Photo 10", "Photo 11", "Photo 12", "Photo 13", "Photo 14"]
func collectionView(collectionView: UICollectionView, numberOfNumberItemsInSection: Int) -> Int {
return cellImages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: PhotoViewCell = collectionView.dequeueReuseableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as PhotoViewCell
cell.labelCell.text = cellLabels[indexPath.row]
cell.ImageCell.image = UIImage(named: cellImages[indexPath.row])
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedImage = cellImages[indexPath.row] as String
selectedLabels = cellLabels[indexPath.row] as String
self.performSegueWithIdentifer("showDetail", sender: self)
}
override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
if segue.identifer == "showDetail" {
let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
detailVC.selectedImageName = selectedImage
detailVC.selectedLabel = selectedLabels
}
}
}
PhotoViewCell
class PhotoViewCell: UICollectionViewCell {
#IBOutlet var labelCell: UILabel!
#IBOutlet var ImageCell: UIImage!
}
EDIT 3 - Amended
I tried your suggestion and unfortunately the problem is still persisting on double views - it's still passing two views before it takes me to the actual selected cell. I also amended the code slightly in the didSelectItemAtIndexPath but it still didn't fix the problem.
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? PhotoViewCell {
performSegueWithIdentifier("showDetail", sender: cell)
}
However following your other suggestion, in my StoryBoard I have added a segue from my Collection View cell to my DetailViewController, which has the identifier "showDetail". If I remove segue nothing can be selected from my cells.
Although it seems the performSegueWithIdentifer code is the trigger for the double views because when I remove it, the cell is only being selected once, the problem was that the indexPath of the cell selection was not correct, because it's first selecting on a blank view (is that to do with the showDetail segue?), which then puts my indexPath out of sync.
EDIT - Solved
This stopped the double selections (the performSegueWithIdentifier line was removed): -
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cellLabels[indexPath.row] as String
cellImages[indexPath.row] as String
}
}
Many Thanks for your help !!!!

(NOTE: I updated this for Swift 4 and more modern practices.)
I stick to UIView objects as much as possible.
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) else { return }
performSegue(withIdentifier: "showDetail", sender: cell)
}
Then in prepare(for:sender:)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "showDetail":
guard let indexPath = (sender as? UIView)?.findCollectionViewIndexPath() else { return }
guard let detailViewController = segue.destination as? DetailViewController else { return }
detailViewController.selectedImageName = cellImages[indexPath.row]
detailViewController.selectedLabel = cellLabels[indexPath.row]
default: return
}
}
I used an extension I created a while ago findCollectionViewIndexPath()
extension UIView {
func findCollectionView() -> UICollectionView? {
if let collectionView = self as? UICollectionView {
return collectionView
} else {
return superview?.findCollectionView()
}
}
func findCollectionViewCell() -> UICollectionViewCell? {
if let cell = self as? UICollectionViewCell {
return cell
} else {
return superview?.findCollectionViewCell()
}
}
func findCollectionViewIndexPath() -> IndexPath? {
guard let cell = findCollectionViewCell(), let collectionView = cell.findCollectionView() else { return nil }
return collectionView.indexPath(for: cell)
}
}
I have a suspicion that you have a segue in the storyboard already and don't need func collectionView(, didSelectItemAtIndexPath:), but either way, the prepare segue should work.

Swift 3.0
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let iPath = self.collectionView.indexPathsForSelectedItems
let indexPath : NSIndexPath = iPath![0] as NSIndexPath
let rowIndex = indexPath.row
print("row index = \(rowIndex)")
}

Related

Perform a segue from a UICollectionViewCell to show a detail screen not working

I have a ViewController that contains a UICollectionView where each cell is a custom cell. I need to perform a segue, so when the user taps over any of the cell, a new ViewController is shown for showing the detail of the pressed cell.
With the code I have right now, the segue is performed if I press over a cell with two fingers, but not with just one. Moreover, when the DetailsViewController is show, the title is not updated.
I cannot post images, but the segue I create in the storyboard goes from the cell to the DetailsViewController, is type Show (e.g. Push) and the id is showDetail.
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib.init(nibName: "MovieCell", bundle: nil), forCellWithReuseIdentifier: "MovieCell")
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "showDetail") {
guard let viewController = segue.destination as? DetailsViewController else {return}
guard let indexPath = sender as? IndexPath else {return}
viewController.movie = self.moviesList[indexPath.row]
}
}
DetailsViewController.swift
class DetailsViewController: UIViewController {
var movie: Movie? = nil {
didSet {
navigationController?.title = movie?.title
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Any idea what is going wrong?
Change sender to the collectionView cell:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: MovieCell)
}
Use this in prepare for segue:
if(segue.identifier == "showDetail") {
guard let viewController = segue.destination as? DetailsViewController else {return}
let cell = sender as MovieCell
guard let indexPath = self.collectionView!.indexPathForCell(cell) else {return}
viewController.movie = self.moviesList[indexPath.row]
}

Passing data between ViewControllers depends on tag of UIButton

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
}

Second segue from TableViewController creating SIGABRT error relating to the first

I have created a NoteBook application within a larger app. I have all the functionality working including a segue to an Add Note page which triggers programatically from clicking a note (to edit it) or a + barButtonItem.
I need a second segue to send the user back to the home page of the app, but every way I seem to try it conflicts with the existing segue I have in place.
Can anyone suggest a way to get the second segue to work. They both have different identifiers which I am referencing in the methods. Its just the goHome segue that will not work...
class NoteBookViewController: UITableViewController, NoteViewDelegate {
func didUpdateNoteWithTitle(newTitle: String, andBody newBody: String) {
self.noteBookEntries[self.selectedIndex] ["title"] = newTitle
self.noteBookEntries[self.selectedIndex] ["body"] = newBody
self.tableView.reloadData()
saveNotesArray()
}
var noteBookEntries = [[String:String]] ()
#IBAction func newNote() {
var newNote = ["title" : "", "body" : ""]
noteBookEntries.insert(newNote, at: 0)
self.selectedIndex = 0
self.tableView.reloadData()
saveNotesArray()
performSegue(withIdentifier: "editNoteBookSegue", sender: nil)
}
var selectedIndex = -1
func saveNotesArray() {
UserDefaults.standard.set(noteBookEntries, forKey: "notes")
UserDefaults.standard.synchronize()
}
override func viewDidLoad() {
super.viewDidLoad()
if let newNote = UserDefaults.standard.array(forKey: "notes") as? [[String:String]] {
noteBookEntries = newNote
}
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.reply, target: self, action: #selector(NoteBookViewController.navigateToNextViewController))
}
func navigateToNextViewController(){
self.performSegue(withIdentifier: "goHome", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return noteBookEntries.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell (withIdentifier: "CELL")! as UITableViewCell
cell.textLabel?.text = noteBookEntries[indexPath.row]["title"]
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
noteBookEntries.remove(at: indexPath.row)
UserDefaults.standard.set(noteBookEntries, forKey: "notes")
self.tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndex = indexPath.row
performSegue(withIdentifier: "editNoteBookSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
let notesEditorVC = segue.destination as! NewNoteBookEntryViewController
notesEditorVC.navigationItem.title = noteBookEntries[self.selectedIndex] ["title"]
notesEditorVC.noteBodyText = noteBookEntries[self.selectedIndex] ["body"]
notesEditorVC.delegate = self
}
}
error message relating to original segue - this segue works until the second is added
In your prepareForSegue method first line is:
let notesEditorVC = segue.destination as! NewNoteBookEntryViewController
This method is called for each of your segues. When it is called for your first segue it works totally fine because the destination view controller is, in fact, of type NewNoteBookEntryViewController.
However, when this method is called for your second segue, the destination controller is of different type. So, you get a crash when you force downcast it.
You should add some logic to your prepareForSegue method so that you distinguish between segues. For example:
if segue.identifier == "addNote" {
let notesEditorVC = segue.destination as! NewNoteBookEntryViewController
//some other code
}
Solved by adding the logic and then adding this to the destination controller:
override func viewWillAppear(animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: true)
}

PrepareForSegue from a UIButton in a custom prototype cell

As the title say I have a tableView with prototype cell; cell is a custom cell (so I made a class called CustomCell.swift in witch I created the IBOutlet for image, label, button etc); here my class
import UIKit
class CustomCell: UITableViewCell
{
#IBOutlet var imageSquadra: UIImageView!
#IBOutlet var button: UIButton!
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
}
}
then I made the UITableViewController:
import UIKit
class SquadreController: UITableViewController
{
var index: NSIndexPath?
var isScrolling = Bool()
override func viewDidLoad()
{
super.viewDidLoad()
DataManager.sharedInstance.createCori()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return DataManager.sharedInstance.arrayCori.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
cell.backgroundColor = UIColor.clearColor()
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.greenColor()
}
else
{
cell.backgroundColor = UIColor.blueColor()
}
//Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
//Following 4 method are used to detect UIScollView scrolling and to change cell height.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
index = indexPath
tableView.beginUpdates()
tableView.endUpdates()
}
override func scrollViewWillBeginDragging(scrollView: UIScrollView)
{
isScrolling = true
tableView.beginUpdates()
tableView.endUpdates()
}
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
isScrolling = false
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if isScrolling
{
return 100
}
if index == indexPath
{
return 200
}
else
{
return 100
}
}
//Here I implemented the makeSegue() func, the action I had made as target of the button.
func makeSegue() {
self.performSegueWithIdentifier("toCoriViewController", sender: self)
}
}
Ok ok now comes the hard part: to make the prepareForSegue; I do not have any ideas how to solve this problem, I tried
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
if let indexPath = ?????????????
{
let controller = segue.destinationViewController as! CoriViewController
controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row]
}
}
}
but I don't know how to set the constant indexPath.
Oh, first of all I made a segue by control-right from the button to the second controller: maybe I must make that segue from the tableView cell???
Hope someone could help me!
You could get the index path of the cell like so
let indexPath : NSIndexPath
if let button = sender as? UIButton {
let cell = button.superview?.superview as! UITableViewCell
indexPath = self.tableView.indexPathForCell(cell)
}
You'll also need to change your makeSegue like this:
func makeSegue(button:UIButton) {
self.performSegueWithIdentifier("toCoriViewController", sender: button)
}
and in your cellForRowAtIndexPath just change the line where you set the action to cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside).
Alternatively, you could create a squadra property inside your custom cell class to hold the arrayCori value of that cell, so you'd have some code that looks like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
cell.squadra = squadra #add this line
cell.backgroundColor = UIColor.clearColor()
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.greenColor()
}
else
{
cell.backgroundColor = UIColor.blueColor()
}
//Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
let controller = segue.destinationViewController as! CoriViewController
if let button = sender as? UIButton {
let cell = button.superview?.superview as! CustomCell
controller.coriSquadra = cell.squadra
}
}
}
Storyboard + prepareForSegue
It can be done with just about no code by adding a separate UIStoryboardSegue with its own identifier for the button in the Storyboard.
prepareForSegue becomes:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if "fromButtonToViewController" == segue.identifier {
if let button = sender as? UIButton {
// ... The UIButton is the sender
}
}
}
This handles both tap on cell and tap on button either jointly or separately, passes the proper sender to prepare:segue:sender, thus allowing customization of the cell, the button, the transition, and ultimately the target view. The demonstration of this statement can be found in the compact project below.
► Find this solution on GitHub and additional details on Swift Recipes.
For those who are looking for a generic approach.
/* Generic function to get uitableviewcell from any UIKit controllers which stored in deep level of views or stackviews */
func getCell<T>(_ view: T) -> UITableViewCell? {
guard let view = view as? UIView else {
return nil
}
return view as? UITableViewCell ?? getCell(view.superview)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
if let button = sender as? UIButton,
let cell = getCell(button),
let indexPath = tableView.indexPath(for: cell)
{
let controller = segue.destinationViewController as! CoriViewController
controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row]
}
}
}

Send data from TableView to DetailView Swift

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!!