navigationController?.pushViewController fails to launch the view - ios

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)}

Related

How to pass to the SeconViewController as the list must show broad list of text in the FirstViewController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let category = result?.data[indexPath.row]
let vc = ListViewController(items: result!.data)
vc.title = category?.title
vc.navigationController?.pushViewController(vc, animated: true)
}
ListViewController is the second Vc as i want to show category of result in there
First Vc works when it opens up but when i click the cell of tableview it doesnt go to ListViewController.
Thank you in advance

UITableView reselects row when scrolling

I've got a tableView with static cells. The modalCell (see screenshot) has a segue to a viewController that the tableViewController presents modally. If I dismiss the modal view, the cell gets deselected. However, when I scroll the cell out of the screen and scroll back so that it appears again, the cell is selected again. I reproduced this behavior in a fresh example project.
Here is my storyboard setup (nothing fancy):
My Code in ViewController looks like so:
class ViewController: UITableViewController {
#IBOutlet weak var modalCell: UITableViewCell!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath), cell == modalCell {
modalCell.setSelected(false, animated: true)
}
}
}
In the screen recording, you can see that the cell deselects correctly. However, if I scroll, the cell is selected again. Any ideas why?
Just deselect cell in table view
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}

UITableView Cell takes a few taps to present view controller

Essentially, with the code below I have the user select on any particular row and have another ViewController shown. While this does work correctly when the app first launches, after pressing and having the ViewController present a few times successfully, so often I will have to press on the cell 2 or 3 times before the ViewController is shown.
Does this have something to do with my custom UITableViewCell class?
The following the code I am using in the TableViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")
}
}
End of didSelectRowAt do
tableView.deselectRow(at: indexPath, animated: false)

TableView Cell not loading View Controller until Different TableView Cell is clicked

I've searched around and haven't truly found why this is happening. Basically, I followed this tutorial https://www.youtube.com/watch?v=yupIw9FXUso by Jared Davison on creating Table View Cells to Multiple View controllers. In his example, everything works perfectly, but for some reason when you click on a table view cell in my code the cell is highlighted in grey. Then, when the user clicks on a separate table view cell the view controller that should have been loaded by the first table view cell is loaded. If the user then clicks back on the original table view cell the page that should have been loaded by the second table view cell is loaded. In summary, all of the view controllers are loaded a "click" behind.
Here is the code for the table view:
//Feed Navigation Functions
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as! FeedTableViewCell
cell.txtTitle.text = "The Fight Against \(elements[indexPath.row])"
cell.issueIcon.image = UIImage(named: "Issue Icons_\(elements[indexPath.row])")
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
Update: The array is a simple array of strings for example [One, Two, Three] there are 6 strings in the array.
When you select a cell then select another one the method didDeselectRow is called so the Vc is pushed you actually want to implement didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
// this to deSelect it after push
tableView.deselectRow(at: indexPath, animated: false)
}
This method fired when a cell is selected
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath
This method fired when a cell is deSelected
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath
You want to use the tableView's delegate method didSelectRow instead of didDeselectRow I think...
Issue: click on a table view cell in my code the cell is highlighted in grey
This is due to the selectionStyle, which you can read about here. If you don't want the cell highlighted, you can set cell.selectionStyle = .none.
Edit: As indicated in other correct response - issue was with incorrect/typo in method - we should use didSelectRowAt not didDeselectRowAt.

Swift - Open ViewController on Tapping a TableViewCell

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.

Resources