0

I've been following the guide in the following (StackOverflow question), which has been very helpful, and by completing the steps I've managed to successfully have a UITableView with a separate controller and delegate/datasource.

However, even though everything is working, I'm unable to reference or change anything instance of the UITableview.

For example: let's say the UIViewController for the table is TableViewController and the separate delegate/datasource class is TableDelegate. I have everything hooked up in interface builder as in the previous SO question. In TableDelegate.m, within viewDidLoad, I put the following:

self.navigationItem.leftBarButtonItem = self.editButtonItem;

And nothing changes in the UITableView as it should. This includes pushing UIViewControllers, which also has no effect as if the code wasn't even there, although when I NSLog(), it appears it is being run.

The odd part, however, is that even though that doesn't work, if I set the table's alpha property to 0, it works.

Thanks for any help in advance, let me know if you need any more information to solve the problem.

Community
  • 1
  • 1
element119
  • 7,475
  • 8
  • 51
  • 74

1 Answers1

0

From your comments, it looks like both TableViewController and TableDelegate are subclasses of UIViewController. If this is the case, make sure both are being added to the navigation stack by pushing them onto a UINavigationController or adding them to a UITabBarController: otherwise, viewDidLoad may not be called.

As an aside, I don't think it's a good idea to have your UITableViewDataSource be a UIViewController subclass when you've already got a UITableViewController to handle that table view.

conmulligan
  • 7,038
  • 6
  • 33
  • 44
  • I would go so far as to say it's an absolutely terribly idea. Either have your delegate be the UIViewController or make it derive from NSObject – Andrew Grant Apr 16 '11 at 23:33
  • @Andrew Grant: Since the delegate/datasource isn't the File's Owner (UIViewController) in this case, it derives from NSObject as a `UITableViewController`. However, the problem still isn't fixed as I'm unable to change anything about the UINavigationController of the table view from the `TableDelegate` file. – element119 Apr 17 '11 at 00:17
  • @Chromium, you should make `TableDelegate` derive from NSObject and set it to conform to the `UITableViewDelegate` and `UITableViewDataSource` protocols instead of `UITableViewController`. A `UITableViewController` is a view controller, and is supposed to be pushed to onto a `UINavigationController` or a `UITabBarController`. – conmulligan Apr 17 '11 at 00:21
  • @conmulligan: I understand your logic- however, how can I implement things like `(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath` and pushing views onto the stack, if it's not a view controller? – element119 Apr 17 '11 at 00:23
  • You could reference the `TableViewController`'s navigation controller and push views onto that, but that's messy. At the very least, `TableViewController` really should be the `UITableViewDelegate`; is there a good reason why it isn't? – conmulligan Apr 17 '11 at 00:33