Reputation: 3
I am using XCode 6, targeting iOS 8 and coding this in Swift.
My storyboard controllers look like this:
Tab Bar > Navigation > Table View > Detail View
The Show Detail
segue is from a Table Cell
to the Detail View
.
The prepareForSegue
method is not triggered when clicking a table cell. Segue from a button to detail works fine, though. performSegueWithIdentifier
in didSelectRowAtIndexPath
also works fine.
I have also created a brand new test project to test this issue - the controller code looks like this:
import UIKit
class TestTableController: UITableViewController
{
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 2;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellId = "testCell";
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell;
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId);
}
cell!.textLabel!.text = "cell \(indexPath.item)";
return cell!;
}
}
Any idea why this does not work out of the box?
P.S.: when using Split View
instead of a Tab Bar
, the same segue works fine.
Upvotes: 0
Views: 755
Reputation: 201
This happened with me when I was registering cell in ViewDidLoad method using tableView.register(AnyClass?, forCellReuseIdentifier: String)
When i commented this line and added the cell class and cell identifier in storyboard, it started working. Again if I uncomment that code in viewdidload, it stops working.
Upvotes: 1
Reputation: 2380
for calling the method prepareForSegue you must have to add a segue to connect the prototype cell and the destination Controller! method doesn't called if you navigate to destination controller programmatically!
Upvotes: 0
Reputation: 80271
I just tested it with a sample project. It works fine. There must be something wrong with your wiring up the cell.
Here is my code. Only indicating changes from plain vanilla tab bar project template.
// FirstViewController.swift
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel?.text = "Cell \(indexPath.row + 1)"
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let detail = segue.destinationViewController as DetailController
let cell = sender as UITableViewCell
let indexPath = self.tableView.indexPathForCell(cell)
detail.detailItem = "Cell \(indexPath!.row + 1)"
}
//DetailController.swift
class DetailController: UIViewController {
var detailItem : String!
@IBOutlet var label : UILabel!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
label.text = detailItem
}
}
Upvotes: 0