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:
Sounds like you need to create a pointer to your table view object in your UIViewControllerClass.
@interface YourClass : UIViewController
{
IBOutlet UITableView *MyTableView;
}
@property ... IBOutlet UITableView *MyTableView;
...and then hook this up to your "table view" within IB. Such that in prepareForSegue you can then access the UITableView object.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *selectedIndexPath = [self.MyTableView indexPathForSelectedRow];
...
}
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:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let myOtherView = self.storyboard!.instantiateViewControllerWithIdentifier("otherViewController") as! otherViewViewController
myOtherView.url = NSURL(string:"\(array[indexPath.row])")
self.presentViewController(myOtherView, animated: true, completion: nil)
}
Hope that helps.
I'm also new to iOS, but for what it's worth this:
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
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 a UITableViewController
? Your class needs to subclass UITableViewController
for tableView to exist.
After:
DetailViewController *detail = [segue destinationViewController];
put in:
[detail view];
then:
detail.primary = [datasource objectAtIndex:selectedIndexPath.row];
detail.seconday = [datalist objectForKey:detail.primary];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With