2

I have a custom UITableViewCell with a button inside to segue the data to another viewController. This is by utilizing the

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //cell setup code

    cell.button.tag = indexPath.row
    cell.button.addTarget(self, action: "editRow", forControlEvents: .TouchUpInside)

    return cell
}

then using the function

func editRow() {
    self.performSegueWithIdentifier("editRow", sender: self)
}

the editRow segue is a storyboard segue.

The issue...Instead of performing the segue and passing the data, it fires the segue twice and doesn't transfer any data to the "SLEdit" viewController. Any ideas as to why this is happening? Please keep in mind I'm not very familiar with objective-c due to beginning learning with swift. If you need any further information or more code let me know. Thanks!

EDIT:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if segue.identifier == "editRow" {

        let cell = sender as! SLTableViewCell
        let indexPath = tableView.indexPathForCell(cell)
        let SListController:SLEdit = segue.destinationViewController as! SLEdit
        let items:SList = frc.objectAtIndexPath(indexPath!) as! SList
        SListController.item = items
    }
}
Will Zimmer
  • 111
  • 10

1 Answers1

3

This is because you have already made a segue action in storyboard editor and you are calling the same segue again by assigning custom action to the button.

And to pass data to the next view controller use this method:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
    if segue.identifier == "segueIdentifier" {

    }
}

Edit 1:

The error: "Could not cast value of type UIButton" is due to this line:

let cell = sender as! SLTableViewCell

Here the sender UIButton and you are convertring/assigning(whichever term is suitable) to the TableViewCell.

Edit 3:

Replace your code by this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!{

    if segue.identifier == "editRow" {
        let button = sender as! UIButton
        let view = button.superview!
        let cell = view.superview as! <Your custom cell name here>

        let indexPath = itemTable.indexPathForCell(cell)

        let SListController:SLEdit = segue.destinationViewController as! SLEdit
        let items:SList = frc.objectAtIndexPath(indexPath) as! SList
        SListController.item = items
    }
}
Bista
  • 7,869
  • 3
  • 27
  • 55
  • Okay, I think I corrected the segue issue by setting the segue to the same prepareForSegue instead of trying to use another one. It still throws the error of "Could not cast value of type UIButton" and crashes. – Will Zimmer Jan 13 '16 at 07:51
  • Check this similiar issue:[Could not cast value of type 'UITableViewCell'](http://stackoverflow.com/questions/29812168/could-not-cast-value-of-type-uitableviewcell-to-appname-customcellname) – Bista Jan 13 '16 at 07:56
  • Also try removing code inside `if segue.identifier == "editRow" {` block and print the value of `sender`, see what is the output. – Bista Jan 13 '16 at 08:03
  • and when I change the sender as! SLTableViewCell to UIButton it throws an error on the (cell) in the indexPath declaration and then the (indexPath) on the line let items:SLitems = – Will Zimmer Jan 13 '16 at 08:28
  • > is what is printed. So it is definitely the code in that block. and it is still firing twice....im so confused... – Will Zimmer Jan 13 '16 at 08:35
  • I thing: are you connecting your segue with button---->to the view controller in your storyboard? If so remove these line of codes : ` cell.button.addTarget(self, action: "editRow", forControlEvents: .TouchUpInside)` and see what happens. – Bista Jan 13 '16 at 09:04
  • I removed the code and used the story board segue. Worked and only sent it once until I unblocked the code for the segue. It is still crashes and throws the fit over the sender as SLTableViewCell instead of UIButton. However, when I change it SLTableViewCell to UIButton the line 'let indexPath = tableView.indexPathForCell(cell)' the (cell) at the end is errored. I deleted that and 'let items:SList = frc.objectAtIndexPath(indexPath!) as! SList' the (indexPath!) is errored. – Will Zimmer Jan 13 '16 at 09:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100557/discussion-between-the-ub-and-will-zimmer). – Bista Jan 13 '16 at 09:27