Prepareforsegue Not Called from Custom UItableviewcell

Custom UITableViewCell not calling prepareForSegue

You need to implement

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:nil];

}

prepareForSegue not being called in uitableviewcontroller

You are using wrong method instead of your method use this below method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

you are missing "sender" that's why your method not being called.

Segues can be triggered from multiple sources, you can use the information in the segue and sender parameters to disambiguate between different logical paths in your app Check this

PrepareForSegue from a UIButton in a custom prototype cell

You could get the index path of the cell like so

let indexPath : NSIndexPath
if let button = sender as? UIButton {
let cell = button.superview?.superview as! UITableViewCell
indexPath = self.tableView.indexPathForCell(cell)
}

You'll also need to change your makeSegue like this:

func makeSegue(button:UIButton) {
self.performSegueWithIdentifier("toCoriViewController", sender: button)
}

and in your cellForRowAtIndexPath just change the line where you set the action to cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside).

Alternatively, you could create a squadra property inside your custom cell class to hold the arrayCori value of that cell, so you'd have some code that looks like this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
cell.squadra = squadra #add this line
cell.backgroundColor = UIColor.clearColor()
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.greenColor()
}
else
{
cell.backgroundColor = UIColor.blueColor()
}

//Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)

return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
let controller = segue.destinationViewController as! CoriViewController
if let button = sender as? UIButton {
let cell = button.superview?.superview as! CustomCell
controller.coriSquadra = cell.squadra
}
}
}

prepareForSegue not getting called when registering a custom cell

The problem is caused by registering the class of the cell in viewDidLoad,

[self.tableView registerClass:[MMTableViewCell class] forCellReuseIdentifier:@"Cell"];

You setup the segue from a cell in the storyboard, but by registering your class, the table view gets its cell from the class definition, not from the one in the storyboard that has the segue attached to it. To fix the problem, just delete that line.

The documentation for this is somewhat incomplete. You should only register the class if the cell is made completely in code. The fact that you're using a custom cell, doesn't change this fact -- if you have a cell in the storyboard, and you set it to a custom class, you shouldn't register anything.

Swift Prepare(for: segue) not called

You are calling performSegue on self.navigationController, so it's the navigationController.prepareForSegue that will be called, not the one in your VC. You should reconnect the segue to your VC in the storyboard, give it the identifier, and call:

self.performSegue(...)


Related Topics



Leave a reply



Submit