UITableView Cell takes a few taps to present view controller - ios

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)

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

How to push a navigation view using collectionView didSelectItemAt?

I am writing a code for my school project. As a beginner would, I looked up on tutorials on youtube how to code.
To summarize the hierarchy of my used objects:
Main collectionView (first one loaded) for scrolling horizontally between views.
List collectionView for listing cells.
collectionViewCell cells for listing information.
However, I am unable to find a way so that when I call a didSelectItemAt function by tapping on one of the cells to push a new view. I have tried creating a function that pushes the view in the MainViewController and call it by creating an instance of the class in didSelectItemAt function but I had no luck.
have you added datasource and delegate?. if you added please check cell click working or not. if click is working add bellow navigation code.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(controller, animated: true)
}
Contributing to Raj's answer, if you want to push a view controller when selecting some specific item in your collection view, just a slight variation of condition will do the trick, this delegate method is called with you click on an item in your collection view:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == **<your item's index>** {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(controller, animated: true)
}
}
Add the upper Side the Class like this :
class tableViewVC : UIViewController, UITableViewDelegate, UITableViewDataSource {
}
Add inside ViewDidLoad():
tableView.delegate = self
tableView.datasource = self
And then you will use in TableView Method like this :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let ViewController = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
self.navigationController?.pushViewController(ViewController!, animated: true)
}

How to create clickable cells in UITableView (Swift)

I'm working on a personal project and I want to make the cells in my UITableView clickable, such that when they are pressed they segue to the next view, and pass information from their cell along with it when they are pressed.
I've attempted to look over other guides and videos that demonstrate how to do this, however they are all outdated. One that stood out to me that seemed to show promise but didn't end up working was in reference to a certain "didSelectRowAt" function, but I could not get this to work.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as!
CandidateTableViewCell
if (indexPath.row >= candidateCells.count){
print("???")
candidateCells.append(cell)
print("Strange error")
}
tableView.delegate = self
cell.CandidateName?.text = candidatesNames[indexPath.item]
cell.CandidatePos?.text = candidatesPositions[indexPath.item]
//cell.candidateButton.tag = indexPath.row
cell.CandidateName.sizeToFit()
cell.CandidatePos.sizeToFit()
print(candidateCells)
print(indexPath.row)
print(candidateCells.count)
print(indexPath.row >= candidateCells.count)
candidateCells[indexPath.item] = cell
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "Segue", sender: Any?.self)
}
What I was expecting to happen was that the cell would become clickable, and when the cell is clicked it would send me to the next page in the app, however when clicked, nothing happens. The cell does not become highlighted, and the segue does not occur. Thank you so much for any and all suggestions!
If you want to pass information to the next cell, I usually avoid using a Segue, and setup the ViewController with a reuse identifier. By doing this, you can pass information easier. For instance
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YOURRESUEIDENTIFIER") as! YOURVIEWCONTROLLERCLASS
vc.infoToPass = cellData[indexPath.row]
DispatchQueue.main.async {
self.navigationController?.pushViewController(vc, animated: true)
}
}
Your other option would be to implement the shouldPerformSegueWithIdentifier() method in order to set the data properly. You would click your cell, which calls the performSegue function, which would call the shouldPerformSegueWithIdentifier where you can set the data and return true

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

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