views:

505

answers:

1

Hi,

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...

A: 

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.

Tom Irving
Sorry was wrongly pasted. See updated version, also I don't use IB
bresc
Are you sure you're using a UIViewController and not a UITableViewController? There should be no reason for there to be a UITableView in a UIViewController.
Tom Irving
Yes I'm pretty sure
bresc
I'm completely stumped then. A UIViewController shouldn't be creating a UITableView for you.
Tom Irving
Well me too. I'm sure there is some logic error in my code, but I just couldn't find it. So I started a new project... copied all the files needed and rewrote the AppDelegate and the UIViewController in question step by step again... now it works. Sure, I did some things different, but I still have no clue what was causing the problem
bresc