SWRevealViewController nib cell not performing segue - ios

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

Related

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 not displaying at runtime when embedded in a Container View

This is my first time using TableView's and I've already hit a road block.
I've got a View Controller with a Container inside. The Container View has an embedded Table View Controller. I have populated the table view controller using the below code. I have set the datasource and the delegate to the Table View Controller, set the Custom Class of the Table View controller to ORMTableVC and set the Table View cell Identifier to cellIdentifier.
My problem is at runtime the View Controller is just blank and I cannot see any Table lines or data.
Any help will be greatly appreciated.
import Foundation
import UIKit
class ORMTableVC : UITableViewController {
let cellIdentifier = "cellIdentifier"
var Array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
Array = ["Reps", "Weight", "RPE", "Fatigue", "Potential Max", "Fatigue Weight"]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
Cell.textLabel?.text = Array[indexPath.row]
return Cell
}
}
StoryBoard Setup
Built App
After looking through many tutorials managed to fix this. Seemed like the code wasn't the issue it was the storyboard setup.
On the Table View Cell the identifier needed to be cellIdentifier not Cell.

TableView cell select event not triggering

I have created a component in XIB file. This file includes 2 components
Label
TableView
I have then linked and set it's File's Owner class to SampleView. I have attached the XIB file view with SampleView.swift file and this file have only following code in it's class:
#IBOutlet var view: UIView!
I have now created a controller file SampleController with protocols UIViewController, UITableViewDelegate and UITableViewDataSource. I have placed the following code in it's init() func to display the custom component:
init() {
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = UIModalPresentationStyle.Custom
view.addSubview(SampleView())
}
I am using this SampleController to programmatically display as a Modal.
These codes does display as the Modal showing Label and TableView. It also populates the data in TableView. The problem is:
When I tap the cell in table, it doesn't trigger the event on first attempt. When I tap another cell then it trigger the previous cell event.
Any idea why is this happening?
Here are 2 functions used for populating and handling cell tap:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL")
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
}
cell!.textLabel?.text = sampleData[indexPath.row]["title"]
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("tapped")
}
Darn it! I was using didDeselectRowAtIndexPath instead of didSelectRowAtIndexPath. That's what will happen when you are programming after midnight.
You need to use a different method to dequeue the cell:
var cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath)
The check for a nil cell is unnecessary, by the way.

How to push to a different UITableView within the same ViewController and navigate back to first UITableView?

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!

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.

Resources