2

I wrote a code with a UILabel with the name of a meteorological station. Later, I added the code to put a UITableView with a grid view like this website explains http://www.recursiveawesome.com/blog/2011/04/06/creating-a-grid-view-in-ios/

The problem is that now the Table view is shown in all screen and the label can't be seen.

How do I make to put the elements in this order?

  1. UILabel
  2. UITableView

Thanks!

2
  • 1
    How are you creating the UITableView? Did you use InterfaceBuilder? Are you doing it in code? Is your view Controller an instance of UIViewController or UITableViewController? Also, do you want the label to scroll away as you scroll your tableview down, or is it supposed to always be up on top?
    – dbrajkovic
    Feb 23, 2012 at 21:25
  • Do you really want to draw a label directly on top of a table? In other words, do you want the label to partially hide the table? Or do you actually want to draw the label above a table? In other words do you want that label to appear closer to the upper side of the screen than the table?
    – Till
    Feb 23, 2012 at 21:26

3 Answers 3

7

1 With a nib:

If you use a nib you can simply size / layout your label and table view so that they are positioned as desired. A UITableView can be made any size and take up any portion of the screen.

2 Without a nib

Create / alloc / initialize your label and table, and then add them to the view:

[self.view addSubview:myTableView];
[self.view addSubview:myLabel];

The magic step to this is that you need to set the frame of both your label and table view. Thats something that is really custom so I cant help you with that without more direction however it may look something like this:

// the number 10 is used for padding purposes
CGSize labelWidth = CGSizeMake(self.view.frame.size.width-20, 1000.0f);
CGSize textSize = [myLable.text sizeWithFont:myLable.font constrainedToSize:labelWidth lineBreakMode:UILineBreakModeWordWrap];
myLable.frame = CGRectMake(10, 10, labelWidth.width, textSize.height);


myTableView.frame = CGRectMake(0, myLable.frame.origin.y+10, self.view.frame.size.width, self.view.frame.size.height - (myLable.frame.origin.y+10));

Please note that calculating frames can be done MANY ways. Also you will probably have to recalculate the frames manually for rotations.

Hope this helps Good Luck!

1
  • With or without a nib, this doesn't work if he used a UITableViewController to create his view. The view property of UITableViewController IS an instance of UITableView.
    – dbrajkovic
    Feb 23, 2012 at 21:35
3

Depending on the answer to my questions in the comments, there are several answers.

  1. If you want the label to always be on top whether or not the tableview is scrolled, make sure you are using an instance of UIViewController, then create your two views. Set the frame appropriately and then add the label view and the table view as subviews to the main view.
  2. If you want the label to scroll away, that's even easier. Your UIViewController can remain a subclass of UITableViewController. UITableView has a property called tableHeaderView. myTableView.tableHeaderView = labelView;
1

You could to like Rachel said

[self.view addSubview:myTableView];
[self.view addSubview:myLabel];

or after placing it

[self.view bringSubviewToFront:myTableView]
1
  • "bringtosuperview" What class is this a method of?
    – dbrajkovic
    Feb 23, 2012 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.