Segue not getting selected row number
The best way to do this kind of thing is not to use the delegate.
Updated Swift 4+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let selectedIndex = tableView.indexPath(for: sender as! UITableViewCell)
// Do your stuff with selectedIndex.row as the index
}
Original Answer
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
// Do your stuff with selectedIndex.row as the index
}
How to get selected table view cell row in segue?
You are not able to get the selected index of the selected cell because you are not actually selecting a cell. You are pressing a button inside the cell.
So, what you do is get a reference to the button, get the button's superview (the cell) and then you can get the indexPath
of that cell.
class TableViewController: UITableViewController {
var indexPathForButton: IndexPath?
@IBAction func buttonPressed(_ sender: UIButton) {
let button = sender
let cell = button.superview!.superview! as! MyCell // The number of levels deep for superviews depends on whether the button is directly inside the cell or in a view in the cell
indexPathForButton = tableView.indexPath(for: cell)
}
Then in prepare(for segue:)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Tips" {
if initialRow == 1 {
print(indexPathForButton)
let tipVC = segue.destination as! KeysController
}
}
}
how to get the last selected row in a UITableView before a segue is executed
Does tableView.indexPathForSelectedRow
give you what you need?
To add a segue from your view controller, right-click on it and choose "Triggered Segues", drag from the 'manual' row to the segue's destination.
Get index of selected row to use in prepare for segue
You must not change the signature of prepare(for
otherwise it's not going to be called. The sender
parameter must be Any?
Cast the sender
parameter to IndexPath
and I recommend a switch
statement
func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == toDetailVCSegue {
let destination = segue.destination as! DetailViewController
let indexPath = sender as! IndexPath
switch indexPath.row {
case 0: destination.itemArray = namesArray
case 1: destination.itemArray = scoresArray
case 2: destination.itemArray = timesArray
case 3: destination.itemArray = completedArray
default: break
}
}
}
Push a segue at selected row swift 3
You haven't set tableview.delegate
to self
.
tableView(_:didSelectRowAt)
is the Table View Delegate
method. Somewhere set this in your viewDidLoad
.
override func viewDidLoad() {
...
tableview.delegate = self
...
}
objectAtIndex not passing selected row in segue
If you are sure the problem is here, then are you sure self.results contain objects of type Car*?
I would suggest you check in the variables watcher that it contains objects of type Car* because it seems you are requesting the property carName from an object of another type.
indexpath row not getting in prepare for segue for detail disclosure
If you have hooked up accessory action in your storyboard then the sender
in prepareForSegue:sender:
will be the cell that that was tapped. This means you can just do something like
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
How to update selected row before segue?
One possibility: Don't implement didSelectRowAtIndexPath:
. Just move that functionality into your prepareForSegue
implementation. That, after all, is what is called first in response to your tapping the cell. Even in prepareForSegue
you can ask the table view what row is selected.
Another possibility: Implement willSelectRowAtIndexPath:
instead of didSelectRowAtIndexPath:
. It happens earlier.
Related Topics
Should Iboutlet Be Weak or Strong Var
Swift - Lazy Var VS. Let When Creating Views Programmatically (Saving Memory)
Duplicating a Particle Emitter Effect in Sprite Kit
Swift Remove Object from Realm
Button Border with Transparent Background in Swift
How to Insert an Image Inline Uilabel in iOS 8 Using Swift
Swift Error When Trying to Access Dictionary: 'Could Not Find Member 'Subscript''
Swift Only -- Reading from Nsinputstream
How to Setup Viewcontroller in Playgrounds
Window Visible on All Spaces (Including Other Fullscreen Apps)
Uitableviewrowaction with Icon and Text
How to Install Package in Xcode via Swift Package Manager
Hide Tab Bar in View with Push
Arkit - How to Put 3D Object on Qrcode