I'm new to iOS development, so go easy on me please :)
Using Xcode 4.2 for iOS 5, I've built a view controller in IB, with a tableview inside it linked to a detail view controller through a push segue (at table view cell level)
I'm wanting to setup the detail pages labels with data from my datasource based on the index of the cell selected. Initially I coded the "didSelectRowAtIndexPath" only to find out that this is called after the segue processing, and found out that I need to call prepareForSegue instead
My problem is, that whatever I try, as tableView is not a passed parameter to prepareForSegue (unlike most other methods I've used thus far), I'm struggling to get access to the tableView within the view controller in order to call the indexPath method, such that I can reference my datasource
I've seen many examples using self, as below, but this results in an error stating that tableView doesn't exist within the view controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *detail = [segue destinationViewController];
detail.primary = [datasource objectAtIndex:selectedIndexPath.row];
detail.seconday = [datalist objectForKey:detail.primary];
}
I've also tried using
ViewController *main = [segue viewController];
Instead of using self, but this results in the same issue
I know that I could rewrite the example to be a Table View Controller instead, but wanted to know if it was possible this way ? As I've seen people use both methods in development
For reference, I was following this tutorial ... http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html
When I got to the section for completing the detail page, I thought, why not use a segue to link the table view cell to the detail page, and code up the copying of the state and capital prior to the detail page being displayed. As you can see from this tutorial, the main controller is a view controller
My understanding was that NOT using an UITableViewController would give any app greater flexibility should it need to develop into something more complex later on
Hope you can help, Izzy
I'm also new to iOS, but for what it's worth this:
worked for me in
prepareForSegue
. Thanks, I was trying to figure out how to get the currently selected row...I'm not sure what you mean by "I've built a view controller in IB, with a tableview inside", but it sounds like your segue source is a
UIViewController
, not aUITableViewController
? Your class needs to subclassUITableViewController
for tableView to exist.SWIFT 2 UPDATE
I "hooked up" the tableView as an outlet from the storyboard in the ViewController.
like this:
@IBOutlet weak var tableView: UITableView!
Just drag your tableView into the ViewController and make it an outlet. Then you can reference its properties such as
self.tableView.reloadData().
This property as well as others that referenced the tableView were giving me errors until I added the
tableView
as a referencing outlet.Add this to your class:
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
The indexPath.row should now work, as well as the segues. You can also use
instantiateViewControllerWithIdentifier
like this:Hope that helps.
I'm also new to iOS, but for what it's worth:
Sounds like you need to create a pointer to your table view object in your UIViewControllerClass.
...and then hook this up to your "table view" within IB. Such that in prepareForSegue you can then access the UITableView object.
After:
put in:
then:
For some reason, just setting the segue isn't enough to get all the properties set up right. You have to add call view on your destinationViewController.