MoonSoo
MoonSoo

Reputation: 385

how can i add a view in tableView using interface builder

hi i'm studying iOS programming.

i have a wondering using interface builder.

i make a tableViewController and also make .xib file

now i can see the UITableView in interface builder.

i have to add a view, called myView, that contains buttons, labels and so on.

i want to myView be set the top of tableView's area, like tableview's header.

so i make a myView, and add buttons, labels, etc.

and i drag that view into UITableView. ok here's fine.

i can see myView is set the top of UITableView in interface builder.

but i run the program, myView doesn't appear.

of course wire up using IBOutlet, and declare property and synthesize.

but i use NSLog like this

if(self.myView == nil)
   NSLog(@"omg it's nil!!!");

i can't understand that NSLog is printed my prompt area.

because i make that view in interface builder!

i can see tableView, of course can see the cells.

why myView doesn't appear??

how can i fix it??

i want to know how can i fix it using interface builder.

please help me

Upvotes: 1

Views: 2308

Answers (3)

Stewart Macdonald
Stewart Macdonald

Reputation: 2142

The only way I've been able to get this to work is as follows:

  1. Add new nib file to project. Lay out the view in the nib file as you want your table footer/header to look.
  2. Set the class of the File's Owner in the nib to the class of your UITableViewController. This is so we can connect the view in this nib to an IBOutlet in the UITableViewController class.
  3. Set a UIView IBOutlet in your UITableViewController to point to the view in the new nib.
  4. In your UITableViewController class, do something like this in viewDidLoad:

(apparently the code won't format correctly if it's directly after a numbered list.)

[[NSBundle mainBundle] loadNibNamed:@"statsFooterView" owner:self options:nil];
self.tableView.tableFooterView = myFooterView;

Upvotes: 0

Alehar
Alehar

Reputation: 639

My experience with XCode 4.3:

you have to first place UITableView and UIView on the same super UIView. Then, when dragging UIView to the top of UITableView you'll notice XCode highlighting the option of inserting your view as footer.

Once done, you can detach UITableView from superview and delete it.

Upvotes: 2

danielbeard
danielbeard

Reputation: 9149

Not sure if this is possible using interface builder, I usually create a view manually and add it to the tableview header in the viewWillAppear method like so:

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 60)];  
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10,self.tableView.bounds.size.width, 50)];
    label.text = [person getFullName];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:25];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0,1);
    [headerView addSubview:label];

    self.tableView.tableHeaderView = headerView;

A simpler method would be to create a view in a separate nib file and then load it into the table header when you load the tableview like this:

UIViewController *aViewController = [[UIViewController alloc] initWithNibName:@"MYXIBFILEHERE" bundle:nil];
self.tableView.tableHeaderView = aViewController.view;

Upvotes: 2

Related Questions