Steps taken:
Created new single view project
Deleted default view controller
Added new Table View Controller
Embedded TVC into a Navigation Controller
Added a child TVC
Added custom class files and associated to each TVC
Created a Show segue from the first TVC to the child
Implemented required methods, #of sections, # of rows, cellForRowAtIndexPath
All of the tutorials I have watched and read online only include the steps I show above, and the segues start working fine. However, in my case I can not get it to work until I add the following:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("detail", sender: tableView)
}
I'm totally fine with implementing the didSelectRowAtIndexPath method if it is required. I'm just wondering if I am missing something, because it seems to work automatically in everything I've seen online.
Thanks in advance!
If you click on the segue (storyboard) and on the side menu you have to assign the text "detail" from
performSegueWithIdentifier("detail", sender: tableView)
on the Identifier field.
Like the image below
Edit:
Try the code above, rembember to change NAME_OF_THE_DETAIL_VIEW_CONTROLLER to your Detail ViewController.
In this code we set i set "self" before the performSegueWithIdentifier. And when the button is clicked you set the segue.destinationViewController to your new ViewController.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("detail", sender: indexPath);
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "detail") {
var detailView = segue.destinationViewController as! NAME_OF_THE_DETAIL_VIEW_CONTROLLER
}
}
Thank you all for your contributions. I figured out that the problem was the following was missing from the cellForRowAtIndexPath tableView function.
let cell = self.tableView.dequeueReusableCellWithIdentifier("custom", forIndexPath: indexPath) as! CustomCell
I had this line commented out, mostly because I have no idea what it does. :)
Related
I'm attempting to create 2 different default view controllers that then get called and do their specified action depending on what was in the cell. Would you create the view controller in the main storyboard use an xib, etc?
Basically I have a xml file that I'm parsing and then creating table views for till I get to the last (details) page for the item. From my understanding it is better to use the same tableviewcontroller multiple times instead of creating 1 for each level. Am I supposed to be creating a segue loop?
There are checks currently in place to make sure I use the right segue.
Would you pefrom the segue with:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath as IndexPath)
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
segueLocation = hNTUL[(indexPath as NSIndexPath).row].locationLevel
segueTitle = hNTUL[(indexPath as NSIndexPath).row].locationTitle
if segueLocation == "TableView" {
performSegue(withIdentifier: "defaultDetailSeague", sender: cell)
let destinationVC = DefaultDetailViewController()
destinationVC.detailTitle = segueTitle
}
if segueLocation == "DetailView" {
performSegue(withIdentifier: "defaultTableSeague", sender: cell)
let destinationVC = DefaultTableViewController()
destinationVC.tableTitle = segueTitle
}
}
It is also possible that I'm trying to go about this the wrong way.
I've also been searching for project examples but haven't found any that might guide me in the right direction.
Thoughts?
If you call performSegue, you must not create a destinationVC; the segue creates it for you. To configure the destination view controller, implement prepare(for:sender:) and fetch the segue's destination, casting it down to a DefaultDetailViewController. Be sure to check that this is the right segue first!
Im currently trying to send some information from custom cells to the next view controller in Xcode 7 with Swift 2.
I can send information fine if it is from a label etc but i cannot do it from cells as I'm unable to get the specific cell the user tapped. I have tried to take information from tutortials on how to get the index path but always get an error.
Layout of View Controllers
Code for main VC and error
Any ideas on how to fix this?
You need to create segue like ViewController to ExpanView Make sure
not for UITableViewCell To ExpanView
your tableView.allowsSelection = true And set tableView.delegate = self
Implyment didSelectRowAtIndexPath Method of tableView like this way.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("exapanView", sender: indexPath)
}
implement prepareForSegue method like this way
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "exapanView" {
let selectedIndexPath = sender as! NSIndexPath
let nameString = self.name.objectAtIndex(selectedIndexPath.row)
}
}
Hi I'm creating an app related to iOS. This app display number of people in tableview. Each cell in tableview shows the name of the person and detail of the person also each person have audio file attached. when i select the any cell and hit the shortlist button i want to display this information into another screen and email the selected cell to anyone. So i want to show name, detail, and the audio file into another screen. i am using swift language and Xcode 7. Please help me to find answer.
Please implement TableView Delegate method below, When you select a cell this method will invoke. There you can instantiate new ViewController where you want to display the data.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
First of all you need to find out what cell was selected. Using the didSelectRowAtIndexPath function.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
}
After you have found out what row was selected you can then segue into the next view controller but before this is done you will have to call the prepareForSegue function which allows the variables to be passed into the view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
}
Use didSelectRowAtIndexPath method current ViewController,
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let sample = storyBoard.instantiateViewControllerWithIdentifier("YourViewControllerIdentifierName") as! ViewControllerB
sample.mainArray = yourArrayName.mutableCopy() as! NSMutableArray
self.navigationController?.pushViewController(sample, animated: true)
}
}
If you send data to Another ViewControllerB declare the variable mainArray,
import UIKit
class ViewControllerB: UIViewController {
var mainArray = NSMutableArray()
}
The above example is one way, you will declare the variable's to pass many data tableview didSelectRowAtIndexPath to Another ViewController,
hope its helpful
I have a tableView in my application and when a user taps on that cell, I need it to go to another view controller that gives more details about the cell they clicked on. When I try to get the cellForRowAtIndexPath by using
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
Nothing is printed to the console so I cant pass that data to another view controller through a segue without knowing that. Is there a reason this would not work?
I am getting the information though this array: var postsArray = [PFObject]() and the table is populated using return postsArray.count
Any help would be greatly appreciated. I may be overlooking it but I am not sure why this would not work as it has worked in other applications of mine.
I have imported delegates as well and have also called table.delegate = self to my viewDidLoad method.
class Profile: UIViewController, UITableViewDelegate, UITableViewDataSource
The full code can be found here: http://pastebin.com/26frZGpa
The line
tableView.delegate = self
as suggested in the very first comment is missing, that's the reason why the delegate method is not called. You can connect the delegate also in Interface Builder.
In your case to perform the segue it's more convenient to connect the segue to the table view cell rather than to the view controller.
The major benefit is that the table view cell is passed as the sender parameter in prepareForSegue and you can get the index path easily with
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let selectedIndexPath = tableView.indexPathForCell(sender as! UITableViewCell)!
}
I have a Tab Bar application, and one of the tabs, which contains a Table View, segues into a third view when a table cell is pressed. The view controller acts as a delegate for the UITableView, and I trigger the segue programatically as follows:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("cell tapped, starting segue")
performSegueWithIdentifier("showDetails", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println("prep for segue")
// TODO - more code here
}
Finally, I set up the following code to debug the problem with the third view:
class DetailsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
println("did load")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("will appear")
}
}
The problem is that when I press a table cell for the first time, the viewWillAppear function never gets called until I interact with the UI in some way (e.g. just a tap anywhere on the screen). The view that I want to segue into doesn't show up, as if the screen didn't get refreshed. However, when I tap the screen, the whole animation runs and I can segue as intended. This is my output when I tap a cell:
cell tapped, starting segue
prep for segue
did load
I tried to find solutions online, but all the issues I found it seems to just not work at all. In my case, it is working, but not immediately.
In case it helps, here's a screenshot of my storyboard:
Sefu found the answer and posted it in the comments, I ran into the same issue and his solution worked for me. The trick is to make it so the cell that is selected that triggers the segue needs to have a selection style set (not None), and I also found that deselecting the cell in tableView:didSelectRowAtIndexPath: also needed to happen.
Ran into a similar problem while having my selectionStyle = .None .
An option you can use, if you're like me and don't want a selectionStyle applied is to set the cell item back to unselected in the prep for segue.
That seemed to stopped the 'issue' I was seeing where the segue would work perfectly once, but all subsequent calls would require selecting the cell twice.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//sending the index path up as the sender so the prep for segue can access the cell
self.performSegueWithIdentifier("segueID", sender: indexPath);
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueID"){
if let indexPath : NSIndexPath = sender as? NSIndexPath{
tableViewReference.cellForRowAtIndexPath(indexPath)?.selected = false;
let destinationVC : UIViewControllerClass = segue.destinationViewController as! UIViewControllerClass;
destinationVC.customMethod(/* some value */);
}
}
}