-1

I thought I'd have a go at building an ipad app using swift. The app that I'm mucking around with is a master-detail app. In the master table I have 2 rows: "Window1" and "Window2" and two detail views. I have created a storyboard segue to the two detail views (1 being the default one). The two segues are called "showDetail" and "showWindow2".

A video I was watching on youtube used the following code to direct the user from the master tab to the appropriate detail page:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        var row = indexPath.row

        switch row
        {
        case 1:
            self.performSegueWithIdentifier("showDetail", sender: self)
        default:
            self.performSegueWithIdentifier("showWindow2", sender: self)
        }
    } 

which works when I have only two rows in the master page. The above code doesn't seem to work when I have 3 rows on the master page (by adding a row with the label "Window3"):

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3
        }

        override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
            var row = indexPath.row

            switch row
            {
            case 1:
                self.performSegueWithIdentifier("showDetail", sender: self)
            case 2:
                self.performSegueWithIdentifier("showWindow2", sender: self)
            default:
                self.performSegueWithIdentifier("showWindow2", sender: self)
            }
        } 

The issue I think I'm having is that value of "row" seems to change depending on which row I am currently on. By this I mean when I am debugging, the row value when selecting either "Window1", "Window2", or "Window3" seems to vary between 0, 1, and 2 and my code does not result in the correct detail page being shown.

I don't know what I'm missing here. Can anyone please help me out?

Woody
  • 1,210
  • 4
  • 19
  • 27
  • 2
    I see you are using `didDeselectRowAtIndexPath`. Did you mean `didSelectRowAtIndexPath` ? – marosoaie Apr 06 '15 at 08:56
  • 1
    Segue identifier same for window2 & window3. For window3 also you want to show window2 detail? Also you mean didSelectRowAtIndexPath? – Uttam Sinha Apr 06 '15 at 08:59
  • Thank-you. I selected the wrong method when the autocomplete window popped up....I did mean to use didSelectRowAtIndexPath which fixed my problem. – Woody Apr 06 '15 at 13:22

1 Answers1

0

Your switch statement seems to be wrong.

switch row{
    case 1:
        self.performSegueWithIdentifier("showDetail", sender: self)
    case 2:
        self.performSegueWithIdentifier("showWindow2", sender: self)
    default:
        self.performSegueWithIdentifier("showWindow2", sender: self)
}

In your switch statement you use self.performSegueWithIdentifier(“showWindow2”, sender: self) You use the same Segue as you use for your default row (which will be used if you press the first row).

Changing the case 2 Segue to the Segue that will show your 3rd detail view should solve your problem

Furthermore you could use case 0 to see if somebody pressed your first row. Although your solution will work, if the first row is pressed it will fire your default statement, if you have a case 0 this will be fired if you press the first row instead of your default. This will result in having default to catch any errors

milo526
  • 4,631
  • 4
  • 38
  • 55