I have a problem with indexPathForSelectedRow()
I have 2 viewcontroller:
tableview and view controller
I want to move from tableview to another controller
and I used func prepareForSegue but I have problem in indexpathselectedrow()
This is issue for the problem:
cannot invoke "indexpathforselectedRow" with no arguments
The Xcode is a last version
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "show"
{
let indexpath = tableView.indexPathForSelectedRow()
let detailvc: fiveViewController = segue.destinationViewController as! fiveViewController
}
}
There is the indexPathForSelectedRow definition in Swift 1.2:
- (NSIndexPath *)indexPathForSelectedRow; // returns nil or index path representing section and row of selection.
There is its definition in Swift 2.0:
var indexPathForSelectedRow: NSIndexPath? { get } // returns nil or index path representing section and row of selection.
In Swift 2.0, the indexPathForSelectedRow is a property not a method, so you should change:
let indexpath = tableView.indexPathForSelectedRow()
to
let indexpath = tableView.indexPathForSelectedRow
Sounds like there is no reference to tableView or it's not of type UITableView.
override func prepareForSegue(segue: UIStoryboardSegue , sender: AnyObject?) {
if segue.identifier == "show"
{
let indexpath = self.tableView.indexPathsForSelectedRows()!
let detailv:fiveViewController = segue.destinationViewController as! fiveViewController
detailv.pic = self.menu[indexpath.row].picName
}
this link for the pic " my problem "
Related
I'm trying to send indexPath.row of selected cell in collection view to a destination controller (detail view) and I've done the following so far
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let recipeCell: Recipe!
recipeCell = recipe[indexPath.row]
var index: Int = indexPath.row
performSegueWithIdentifier("RecipeDetailVC", sender: recipeCell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "RecipeDetailVC" {
let detailVC = segue.destinationViewController as? RecipeDetailVC
if let recipeCell = sender as? Recipe {
detailVC!.recipe = recipeCell
detailVC!.index = index
}
}
}
indexPath.row is a type of NSIndexPath so I've tried to convert to Int but I get Cannot assign value of type '(UnsafePointer<Int8>,Int32) -> UnsafeMutablePointer<Int8>' to type 'Int' at runtime
In the destination view controller I've initialized var index = 0
to receive the indexPath.row value
Any idea why I get this error at runtime?
Move the following out of the function and make it a property.
var index: Int = indexPath.row
In prepareForSegue you have the following:
detailVC!.index = index
The variable 'index' isn't declared in the class or locally, so what you get is a function named 'index' which is defined as:
func index(_: UnsafePointer<Int8>, _: Int32) -> UnsafeMutablePointer<Int8>
If you make 'index' a property, it will be used instead of the function of the same name.
It's a collectionView so I believe you should be using indexpath.item not .row
You have the following line in didSelectItemAtIndexPath:
var index: Int = indexPath.row
This declares index as being local to this function only. Then in prepareForSegue you have:
detailVC!.index = index
Since you're not getting a compilation error, index must also be defined somewhere else. It's this somewhere else variable that didSelectItemAtIndexPath should be setting. It is probably just
index = indexPath.row
Another solution could be:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "RecipeDetailVC" {
let detailVC = segue.destinationViewController as? RecipeDetailVC
if let recipeCell = sender as? Recipe {
detailVC!.recipe = recipeCell
detailVC!.index = collectionView.indexPathsForSelectedItems()?.first?.item
}
}
}
Output
The Event ID is: Circle
Could not cast value of type 'ThisLondon.ViewController' (0x9c338) to 'UICollectionViewCell' (0x39dbd1a0).
(lldb)
Yes "Circle" is being output, that is what I selected, great, although I can't get that to pass over the segue to the tableView?!
here is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "selectedTube" {
let detailViewController = segue.destinationViewController as! tubeDetailsTableViewController
if let indexPath = collecView.indexPathForCell(sender as! UICollectionViewCell) {
detailViewController.selectedTube = stationArray[indexPath.row]
}
}
}
I have two table view controllers
InvoiceList view controller
InvoiceShow view controller
I use didSelectRowAtIndexPath method as bellow to get selected table cell specific value
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let rowObject = objects[indexPath.row]
let invoicehash = rowObject["hash_key"]!
}
i need to send invoicehash value to InvoiceShow controller when click the table cell of InvoiceList
i tried to use prepareForSegue function. but it is not applicable because it will trigger before the didSelectRawAtIndexPath function. so when i implemented it, gives the previous click event variable value. not correct one.
Please help me to access invoiceHash variable value from InvoiceShow controller
You will get the selected cell in prepareForSegue method itself.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let selectedIndexPath = self.tableView.indexPathForSelectedRow()!
let rowObject = objects[selectedIndexPath.row]
let invoiceHash = rowObject["hash_key"]!
let invoiceShowViewController = segue.destinationViewController as! InvoiceShowViewController
// Set invoiceHash to `InvoiceShowViewController ` here
invoiceShowViewController.invoiceHash = invoiceHash
}
You can still use a segue if you want and/or already setup on your storyboard.
You just need to connect the two view controllers in Interface Builder directly from one to another.
So, start ctrl-dragging from the controller itself and not from the TableViewCell (take a look at the screenshot)
then use the performSegueMethod with the new segue identifier like this:
self.performSegueWithIdentifier("mySegueIdentifier", sender: self)
and finally, your prepareForSegue method:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegueIdentifier" {
let selectedIndex = self.invoiceTableView.indexPathForSelectedRow
//if element exist
if selectedIndex?.row < myDataSourceArray.count {
let destination = segue.destinationViewController as! InvoiceShowViewController
let invoice = myDataSourceArray[selectedIndex!.row]
destination.invoice = invoice
}
}
}
That's it!
I am working on a UITableView with multiple sections. Each section has a multiple of items driven from different arrays. When I click the cell from any section I want the name of the item to pass to an another view controller. I know how to use the prepareForSegue function with one array, but I need your help with more than one array.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondScene=segue.destinationViewController as! nextPageViewController
if let indexPath=self.tableView.indexPathForSelectedRow{
let array1=TableData[indexPath.row]
secondScene.currentPhoto=array1
}
Assuming your model is laid down like this:
let section1Array = ["row1", "row2"]
let section2Array = ["row1", "row2"]
let section3Array = ["row1", "row2"]
let sectionArray = [section1Array, section2Array, section3Array]
Here, sectionArray derives your sections and section1Array, section2Array & section3Array derives your individual section rows.
Having said that, this is how you would retrieve and send this data to your second scene:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondScene = segue.destinationViewController as! nextPageViewController
if let indexPath = self.tableView.indexPathForSelectedRow {
let currentPhoto = sectionArray[indexPath.section][indexPath.row]
secondScene.currentPhoto = currentPhoto
}
}
If your table has multiple sections, then in prepareForSegue use indexPath.section to find out which section was clicked on, which tells you which of your backing arrays to use. Then select the item from the array using indexPath.row and pass that to destinationViewController as the photo.
Hello StackOverflow,
I'm just picking up swift and trying to implement data being passed between UITableView Cell to a UIViewController which will show a detailed view of the info shown on the tableview, and whenever I test the application on my emulator first time I press a table cell it passes an empty string and then when I try pressing another cell the viewController shows the string that was supposed to be seen earlier.I pasted the code I have for my tableview didSelectRowAtIndexPath below.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var7 = lat[indexPath.item]
var6 = long[indexPath.item]
var5 = items[indexPath.item]
var1 = detail[indexPath.item]
var2 = date[indexPath.item]
var3 = wop[indexPath.item]
var4 = ViewController()
nextView.locationPassed = var1
//self.performSegueWithIdentifier("DetailPush", sender: self)
println("value stored in var1: \(var1)")
//println("The selected indexPath is \(indexPath.item + 1)")
println("The stored id is: \(storeSend)")
}
Here is my implementation for my push segue method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DetailPush"
{
if let crimesView = segue.destinationViewController as? ViewController {
crimesView.locationPassed = var1
//println("The passing address is: \(var1)")
}
}
}
Any idea on why I'm getting data delayed during the segue?
Thank you
Solution Found: I edited my prepareForSegue method with the following and it fixed my issue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Adding the indexPath variable for the selected table Row within the segue
var indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow()!
if segue.identifier == "DetailPush"
{
if let crimesView = segue.destinationViewController as? ViewController {
//Then just pass the data corresponding to the array I created identified by the index of the selected row
crimesView.locationPassed = self.arrayName[indexPath.row]
println("The passing address is: \(self.addressSend)")
}
}
}
I found the solution by watching some online videos and all I did to fix my issue was redefine my prepareForSegue function with:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Adding the indexPath variable for the selected table Row within the segue
var indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow()!
if segue.identifier == "DetailPush"
{
if let crimesView = segue.destinationViewController as? ViewController {
//Then just pass the data corresponding to the array I created identified by the index of the selected row
crimesView.locationPassed = self.arrayName[indexPath.row]
println("The passing address is: \(self.addressSend)")
}
}
}
And it seems to work like a regular segue for me.......Thank you for all the suggestions given me
you said you are doing the prepareForSegue from async request
so try this:
if segue.identifier == "DetailPush"
{
dispatch_async(dispatch_get_main_queue()) {
if let crimesView = segue.destinationViewController as? ViewController {
crimesView.locationPassed = var1
//println("The passing address is: \(var1)")
}
}
}
try to remove the line
tableView.deselectRowAtIndexPath(indexPath, animated: false)
see if it still happens.
maybe move it to the end