didSelectRowAtIndexPath to performSegueWithIdentifier gose wrong but didDeselectRowAtIndexPath is ok? - ios

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?

Related

navigationController?.pushViewController fails to launch the view

Language is Swift 4
I have a tableView which is showing some categories and below each category it has one or more detail rows. When I click on the detail row, I want it to launch another tableView to show the selected detail row, along with some additional information pertaining to the selected detail row.
To this end, I have implemented the following function:
// launch the detail view controller
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailView = DetailVC()
detailView.customInit(detailID: tempViewData[indexPath.section].detailIDs[indexPath.row])
setupTableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.pushViewController(detailView, animated: true)
}
However, when I run the app, while no errors are listed, the detail view does not launch. What am I doing wrong?
Select your first viewController in Storyboard, then embed it into a UINavigationController from the menu like this.
Use self.storyboard?.instantiateViewController(withIdentifier:kCreateFBAccountVCID ) as! DetailVC Instead of DetailVC()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let detailView = **self.storyboard?.instantiateViewController(withIdentifier:kCreateFBAccountVCID ) as! DetailVC**
detailView.customInit(detailID: tempViewData[indexPath.section].detailIDs[indexPath.row])
setupTableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.pushViewController(detailView, animated: true)}

UITableViewController's didSelectRowAtIndexPath doesn't work

I am using an UITableViewController for it and I override the following method and try to enable different segues when the user select different row.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected row #\(indexPath.row)!")
switch indexPath.row {
case 3:
performSegue(withIdentifier: "segue3", sender: self)
case 8:
performSegue(withIdentifier: "segue8", sender: self)
default:
break
}
}
But actually, this method never get called, and the print out never show up when I select a row.
I did enable single selection, enable user interaction and set the delegate and datasource to the controller itself (That's automatically set when using UITableViewController, right?)
I am using static cells and default UITableViewCell. The view controller is UITableViewController.
Update:
I try to replace:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
With:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
And this doesn't work also.
for Swift 3
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
// your code ...
}
and one thing make sure your tableView single selection property is selected ...
Also check the selection of tableView in AttributeInspector of table view in your Storyboard. It should be selected to single selection.
Since you are using a UITableViewController override the function. Otherwise if you are using a tableView in a ViewController make sure the delegate is set to your ViewController class.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected row #\(indexPath.row)!")
switch indexPath.row {
case 3:
performSegue(withIdentifier: "segue3", sender: self)
case 8:
performSegue(withIdentifier: "segue8", sender: self)
default:
break
}
}
Ok from what I’m seeing with your code. You is trying to print a string and also segue at the same time. But which object is you selecting to segue. It should be an array or something else example:
Var array[“Swift”, “IOS”]
DidSelect{
Switch: array[indexpath.row]
Case 3:
Print(“ u selected /(indexpath.row)”)
PerformSegue.........
Case 4 and so on.....

UITableViewController cell remains highlighted when segueing back

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

didSelectRowAtIndexPath not working in staticTableViewCells

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.

In my UITableView, how can I reset the "selected cell color" back to default upon selection?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
checkRequirements() { () -> Void in
var room = self.rooms[indexPath.row]
self.roomForPreview = room
self.indexPathForPreview = indexPath
self.performSegueWithIdentifier(CONSTANTS.SegueLobbyToRoomPreview, sender: self.view)
}
}
I perform a segue when a user selects a cell, but when I unwind, I find that the cell still has a grey background.
How can I make the cell grey only on touch? Once the touch is gone, the cell goes back to normal color.
Use deselectRowAtIndexPath:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
...
If you're using a UITableViewController, you can set the clearsSelectionOnViewWillAppear property to true - this will work as well.

Resources