Subscribe ( )

iPhone Programming Tutorials

 

Forum

You must be logged in to post Login Register

Search 

Load UIView from xib file

User Post

6:40 pm
October 9, 2008


colinhumber

Noob

colinhumber

posts 5

1

I have created a UIView to use as the tableHeaderView on a UITableView. The UIView was created as a simple view in interface builder that contains just a label. I can't figure out how to create the view from the nib file. I've tried the following lines and I keep getting errors:

UIView *view  [[UIView alloc] initFromNibName:@”TestView” bundle:[NSBundle mainBundle];

and

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@”TestView” owner:self options:nil] objectAtIndex:0];

Any thoughts?

11:33 am
October 10, 2008


VertigoSol

Moderator

posts 26

2

make sure that the view has a file owner (viewcontroller) when you load the nib

1:50 pm
October 10, 2008


colinhumber

Noob

colinhumber

posts 5

3

VertigoSol said:

make sure that the view has a file owner (viewcontroller) when you load the nib


I set the file owner to the view controller (UITableViewController) that will contain the UITableView. That works fine, but when I try to assign the created view to the tableViewHeader using self.tableView.tableViewHeader I get an exception.

I created a subclass of UIView that contains an IBOutlet for the label on the view and set the type of the view in IB to that class but it's bombs out.

I'm starting to get a little lost…

3:24 pm
October 10, 2008


VertigoSol

Moderator

posts 26

4

I Think what you need to do is add a subview to the tableViewHeader instead of trying to resassign it

UIView* myView = …load from nib or manually created
[self.tableView.tableViewHeader addSubview:myView];

//release if nessisary

The base uiview header probably has to be there for the table to use it

5:26 pm
October 10, 2008


colinhumber

Noob

colinhumber

posts 5

5

The code runs through fine but nothing appears when the subview is added to the tableHeaderView. The awakeFromNib method is run on the UIView subclass when the view is created, so at least I know something is happening but I'm at a loss as to why nothing is appearing in the header.

12:42 am
October 11, 2008


VertigoSol

Moderator

posts 26

6

here I was messing around with this I got this to work
example with UILabel as the header view

UILabel* label = [[UILabel alloc] initWithFrame:
CGRectMake(0.0, 0.0, 300.0, 20.0)];

label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:20];
label.textColor = [UIColor blackColor];

self.tableView.tableViewHeader = label;

just set frame cords to location you want

9:15 am
October 11, 2008


colinhumber

Noob

colinhumber

posts 5

7

The UILabel as the view works fine by itself. Eventually what I would like is to have the view include a couple of labels and an image. To do that I would need a UIView object that contains those elements, correct? But the UIView doesn't seem to want to display in the header, only individual elements.

I just tried creating the view manually and not from the xib file and all the subviews (labels) are displaying properly. It almost seems like there's a small setting missing from the xib created view to get it to display.

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        description.text = @”TEST”;
        description.backgroundColor = [UIColor clearColor];
       
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 100, 50)];
        label2.text = @”TEST 2″;
        label2.backgroundColor = [UIColor clearColor];
       
        self.backgroundColor = [UIColor greenColor];
        [self addSubview:description];
        [self addSubview:label];
        // Initialization code
    }
    return self;
}


About the iCodeBlog forum

Currently Online:

3 Guests

Maximum Online: 25

Forums:

Groups: 2

Forums: 6

Topics: 305

Posts: 743

Members:

There are 654 members

There are 1 guests


Brandon has made 87 posts

Top Posters:

bobcubsfan - 53

crazyiez - 30

VertigoSol - 26

Uhu - 17

AdeC - 17

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)