2
votes

I have a weird problem.

I'm subclassing UIViewController and adding a tableView property in which I load a UITableView. Now I'm adding this UITableView to the parent's subviews. Works fine but I get TWO TableViews. One standard styled TableView and one the way I wanted it to be on top of the standard one. Both contain the correct data though.

@interface RootViewController : UIViewController <ABPersonViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
  UITableView *tableView;
  ToolbarController *toolbar;
  ...
}

@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) ToolbarController *toolbar;
...

@end

@implementation RootViewController
- (void)viewDidLoad {
  [super viewDidLoad];

  CGRect mainViewBounds = self.parentViewController.view.bounds;
  CGFloat toolbarHeight = 44;

  toolbar = [[ToolbarController alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(mainViewBounds), toolbarHeight) parentView:self]; 

  tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight) style:UITableViewStylePlain];
  tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  tableView.rowHeight = 60.0;
  [tableView tableView].delegate = self;
  [tableView tableView].dataSource = self;
  tableView.backgroundColor = [UIColor clearColor];

  [self.parentViewController.view addSubview:tableView];
  [self.parentViewController.view addSubview:toolbar];
}
@end

------UPDATED------

I just wrongly pasted one part

[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;

is actually in code

tableView.delegate = self;
tableView.dataSource = self;

-------UPDATE 2--------

If I don't create my own UITableView by

tableView = [[UITableView alloc] ...]

then there is one automatically set for me. This would be fine for me... I don't need to init one, but I can't change the frame on the standard one.

tableView.frame = CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight);

Setting frame has no effect whatsoever...

3
Hi @bresc, i take it you never got to the bottom of why the second tableview was turning up in your viewcontroller? I'm having the same problem. So weird. Using the debugger, I see that my viewController's view property is being set to a UITableview object after i present it modally, but before viewDidLoad in the view controller runs.kris
@kris No I never found a solution. My project was pretty much at the beginning when I had the problem, so I just did this part completely different in order to make it work.user287689
Thanks bresc. The only thing i've noticed since is that by adding a nib file and loading the view controller from it seems to make the problem go away. I'll update again if I ever get to the bottom of this.kris

3 Answers

1
votes

i Had the same issue you probably be converting the UITableViewController to UIViewController and Your RootViewController might have the uitableview.

Make the Property of _tableview in your RootviewController with IBOutlet. And link it with the tableview in your XIB .

Don't alloc _tableview in RootviewController and Don't addsubview it .

0
votes

Instead of this,

[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;

Have you tried just using:

tableView.delegate = self;
tableView.dataSource = self;

I don't know why it would be that, but it's the only thing that's standing out. Also make sure you haven't got another UITableView set up in IB.

It might also have something to do with toolbarController. I'm assuming it's a custom class, so make sure you're not creating a UITableView in that.

0
votes

I ran into same issue...

Instead of adding tableview to the view controller directly ([self.parentViewController.view addSubview:tableView];), use following way..

UIView *totalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[totalView addSubview:tableView];
self.view=totalView;

Hope this fixes it (for @kris too!)