2

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

  • 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 Nov 18 '11 at 01:04
  • @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. –  Dec 01 '11 at 19:07
  • 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 Dec 05 '11 at 22:21

3 Answers3

1

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 .

munibsiddiqui
  • 435
  • 5
  • 15
0

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
  • 10,041
  • 6
  • 47
  • 63
  • Sorry was wrongly pasted. See updated version, also I don't use IB –  Mar 18 '10 at 17:21
  • 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 Mar 18 '10 at 18:52
  • I'm completely stumped then. A UIViewController shouldn't be creating a UITableView for you. – Tom Irving Mar 18 '10 at 20:09
  • 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 –  Mar 19 '10 at 09:59
0

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!)

BufferStack
  • 549
  • 9
  • 20