I have a UITableViewController and when a cell is selected, my app segues to a new view controller. When I press the back button in my navigation controller to go back to the previous tableview controller, the cell that I had selected remains highlighted. I checked clearsSelectionOnViewWillAppear in viewWillAppeaer and it is true. Any idea why this is happening?
Are you calling the segue from didSelectRowAtIndexPath if so try something like this
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("Your Segue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
You could also place tableView.deselectRowAtIndexPath the in the prepareForSegue method which is what I tend to do
Related
I have a tableView that the cell have segue to a detailed view controller.
When I use didSelectRowAtIndexPath to perform the detail view controller, I can not dismiss the view controller correctly.
func tableView(tableView: UITableView, **didSelectRowAtIndexPath indexPath**: NSIndexPath) {
performSegueWithIdentifier("addressCell", sender: tableView)
}
After use
dismissViewControllerAnimated(true, completion: nil)
The detailed view dose not dismissed and have warning:
AddAddressViewController: 0x7f891b929d20> on
<AccountAddress: 0x7f89195e3ba0> whose view is not in the window hierarchy!
But when i use didDeselectRowAtIndexPath
func tableView(tableView: UITableView, **didDeselectRowAtIndexPath** indexPath: NSIndexPath) {
performSegueWithIdentifier("addressCell", sender: tableView)
}
It runs correctly and no warning!
What is the problem?
I have a Tab Bar Controller setup. When the user selects a tab, it takes them to a UIViewController. Currently I have a UITableView setup in this View Controller. What I would like to do is, upon selecting a cell from TableView1, I'd like to have a "push" effect where TableView2 comes in from the right side of the screen, and take over.
I've found the following question, similar to mine:
UITableView segue within the same ViewController
One of the suggestion is exactly what I want to achieve, however I could not get it to have that slide effect.
What I've done is added two UITableViews, then tableView1.hidden = true in viewDidLoad.
Then in code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
if tableView == tableView1
{
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("ID1", forIndexPath: indexPath)
....
}
else if tableView == tableView2
{
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("ID2", forIndexPath: indexPath)
....
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if tableView == tableView1
{
tableView2.hidden = false
tableView1.hidden = true
playlistVideosTableView.reloadData()
// Deselects the row
tableView1.deselectRowAtIndexPath(indexPath, animated: true)
}
}
However, it doesn't have that "slide effect" and makes the tableview appears instant.
I could make another UIViewController with the second tableview but I don't want to make my Tab Bar disappear.
How can I achieve this, and how can I ensure the ensure gets back to the first UITableView?
Thanks
I would recommend to simply create another UIViewController that has a UITableView, so preferably a UITableViewController.
Then in tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) you call a segue to the new ViewController. This will give you the desired effect and is a cleaner solution as well.
You will not lose your TabBar either with this solution if you embed all ViewControllers in this tab into a NavigationController.
EDIT: sample code
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("editSpecialStage", sender: tableView.cellForRowAtIndexPath(indexPath))
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell)!
let secondViewController = segue.destinationViewController as! SecondViewController
}
And in the Storyboard you have a NavigationController as the first ViewController from the TabBar. Then the first ViewController and then the second, connected via push segue.
I could make another UIViewController with the second tableview but I don't want to make my Tab Bar disappear.
Why will your tab bar disappear? It wouldn't if you used a UINavigationController!
Just do what I say, alright?
First create that new view controller and connect the two VCs with a show segue
First VC -show-> Second VC
Now the whole picture would look like
Tab Bar VC -view controllers-> First VC -show-> Second VC
Now modify this to
Tab Bar VC -view controllers-> Navigation Controller -root vc-> First VC -show-> Second VC
Now the segue will slide the Second VC to the left and your tab bar won't disappear!
hello I have a TableViewController in my storyboard with static cells. I want to launch different view controller or trigger some methods on each cell clicked. if I drag the segue from my cell to another ViewController it doesn't work and also didSelectRowAtIndexPath also not working.
here is my class
class ProfileTableViewController: UITableViewController {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
print("Row: \(row)") //nothing is printing out
}
override func viewDidLoad() {
super.viewDidLoad()
print("hello")
}
}
If you are adding the TableView in Storyboard, make sure that when you right click the TableView, that the UIViewController which holds the TableView is set as its delegate, or any other object which seems proper for your task.
Simply put, I have a slide navigation view controller in my app with a back table VC and a front table VC. A selected cell in the back table VC is supposed to segue to the front table VC (embedded in a navigation VC). To illustrate, here are the isolated pre-change settings and code with it working properly:
Here is the simplified working code in my back table VC's cellForRowAtIndexPath method:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let navigationCell = tableView.dequeueReusableCellWithIdentifier("NavigationCell", forIndexPath: indexPath) as UITableViewCell
return navigationCell
}
Ok, onto the fun part. All I want is to replace the default UITableViewCell shown above with one of my custom cells in a Xib file. Now I'll illustrate what I modified. First, here's my Xib cell in my NavigationCell.xib file. Nothing special.
My NavigationCell class code is simply
import UIKit
class NavigationCell: UITableViewCell {
#IBOutlet var nameLabel: UILabel!
}
Finally, I modified my code in my back table VC to register the Xib and dequeue the custom cell:
override func viewDidLoad() {
super.viewDidLoad()
let navigationCellNib = UINib(nibName: "NavigationCell", bundle: nil)
tableView.registerNib(navigationCellNib, forCellReuseIdentifier: "NavigationCell")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let navigationCell = tableView.dequeueReusableCellWithIdentifier("NavigationCell", forIndexPath: indexPath) as! NavigationCell
return navigationCell
}
All other code and specifications remain the same. The storyboard segue for the new custom cell is still from the class SWRevealViewControllerSeguePushController as shown in the first screenshot for this question. I simply swapped out the default UITableViewCell with my custom Xib cell.
This modification was enough to stop the segue from occurring. With this modification, after I build and run when I slide out the back table VC navigation menu and select one of my custom navigation cells (which display properly), it doesn't trigger any segue. Any thoughts?
I'm an idiot. I had to perform the segue programmatically in didSelectRowAtIndexPath.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("MySegue", sender: self)
}
I have a tableView with identical cells. After selecting one of them I would like to open another ViewController.
I tried this code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let destination = EditViewController()
navigationController?.pushViewController(destination, animated: true)
}
but it didn't open another ViewController.
Can You give me a hint?
Just go inside you storyboard, Select you initiate view controller, Then click on Editor > Embedded In and select Navigation controller, Then Your code will be work. This mean you have set the navigation stack for your view controllers from initial view controller.
Most probably you just don't have the navigationController there - use rather present to show the viewController:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let destination = EditViewController()
self.present(destination, animated: true, completion: nil)
}
Or if you want the viewController to be pushed, make sure that you embed the tableViewController into a UINavigationController.