2

I'm not sure how to approach this. I'm loading a separate table view controller nib file to my viewcontroller. How do I position it the way I want? Also, Is the below query all I need, or am I missing something? It keeps crashing on me.

- (void)viewDidLoad
{
    [super viewDidLoad];

    HSTableViewController *tableViews = [[HSTableViewController alloc]initWithNibName:@"HSTableViewController" bundle:nil];

    [self addChildViewController:tableViews];
    tableViews.view.frame = CGRectMake(0, 0, 100, 100);
    //tableViews.view.frame = self.view.bounds;
    [self.view addSubview:tableViews.view];
    [tableViews didMoveToParentViewController:self];
user3783961
  • 59
  • 1
  • 8
  • What's the output? Nothing shows up? You will also have to add a `delegate` and `dataSource` –  Aug 14 '13 at 02:01

3 Answers3

2

To add a tableview in code to a view controller wherein that VC will act as both the data source and delegate you would do the following.

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  }
    cell.textLabel.text = @"Hello World";

    return cell;
}

And then in your .H file you need to inform the program that you're going to act as both the Delegate and DataSource

@interface NSSViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

From what I gathered out of your question, that should get you where you need to go. I will edit accordingly if you require further assistance.

Peter Foti
  • 5,526
  • 6
  • 34
  • 47
  • I would like to add a tableViewController as a subview of my UIview..My data and delegate will be coming from a different controller. I figured I needed to add it as a ChildViewController. – user3783961 Aug 14 '13 at 02:24
  • Is the tableview not going to take up the full screen? You can still make self the datasource then instantiate the model with the data in the vc. – Peter Foti Aug 14 '13 at 02:51
  • Peter that sounds like it may be my best bet...I'll give it a try and get back to you. THanks for the suggestion! – user3783961 Aug 14 '13 at 04:30
0

Set your UITableView's frame in -viewWillAppear: like this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    tableViews.view.frame = self.view.bounds;
}
liuyaodong
  • 2,547
  • 18
  • 31
-1

If anyone else needs this. See this tutorial I found on youtube that helped a lot. Thanks for all the help.

http://www.youtube.com/watch?v=DzedmcFlm1c

user3783961
  • 59
  • 1
  • 8