Add UILabel to UITableView - iphone - iphone

I am an iOS development newbie. I have a settings screen which is a UITableView. I want to add some explanation to it. I am using the following code to do it, but it skews up the text completely. Any idea what I am doing wrong?
UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 175)];
subjectLabel.font = [UIFont systemFontOfSize:16.0];
subjectLabel.numberOfLines = 0;
subjectLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(10.0)];
subjectLabel.backgroundColor = [UIColor clearColor];
//bodyLabel.textAlignment = UITextAlignmentLeft;
subjectLabel.text = #"mytext";
settingTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0, 320, 370) style:UITableViewStyleGrouped] autorelease];
settingTableView.dataSource = self;
settingTableView.delegate = self;
[settingTableView addSubview:subjectLabel];
[self.view addSubview:settingTableView];

A tableViewHeader is a UIView which is set as the tableViewHeader property of a tableView. If you want to have a UILabel in a header view, make a separate UIView (either in code, or in a nib), and set it as the tableView.tableHeaderView property. More information can be found here: TableView Reference. Hope that helps!

create a view in your view controller and add your lable to that and bind it ...
IBOutlet UIView *headerView1;
and add this code
settingTableView.tableHeaderView = headerView1;

Suggestion1 : You could have create a separate view which contains your UILabel and place above the UITableView and place your tableView y position would be from the height of the UIView.
Remark : This is useful because when you scroll the tableView the default header will be stick to top.
Suggestion2 : you can use viewForHeaderInSection delegate method. where you can create a view and add the UILabel. viewForHeaderInSection returns the UIView, which you can return your view which contains the UILabel
Remark : when you scroll the tableView the default header will move along with your tableView

Related

How to achieve UIPickerView as shown in image, with titles for components?

How to achieve picker view like this? I have implemented all necessary delegate and dataSource methods to populate the data, but the thing I am not able to understand is how to add this titles adults, children and infants?
They are static and does not spin with the component!
Add the 3 labels to your view as subviews when you showing the picker view and then hiding them when the picker is dismissed.
You will have to position the labels on the band.
I got this done just using Interface Builder.
I created a container view and then put picker view inside it.
To be sure that my container size is the same as picker view I set space constraints: leading, trailing, top and bottom.
Then I put 3 labels above picker view (but they're still the subviews of the container) and set their frames to center it.
Also to achieve the same label visual effect as on the screenshot (it seems to be under selection bar) decrease label's alpha to about 0.7.
You could put the labels on a particular frame position and then make the labels background color as clearColor.
You need to add labels as subviews of the picker view. There is no functionality built into UIPickerView to make this easy.
Create your picker, create a label with a shadow, and push it to a picker's subview below the selectionIndicator view.
It would look something like this
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(135, 93, 80, 30)] autorelease];
label.text = #"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[picker insertSubview:label aboveSubview:[picker.subviews objectAtIndex:5]];
//When you have multiple components (sections)...
//you will need to find which subview you need to actually get under
//so experiment with that 'objectAtIndex:5'
//
//you can do something like the following to find the view to get on top of
// define #class UIPickerTable;
// NSMutableArray *tables = [[NSMutableArray alloc] init];
// for (id i in picker.subviews) if([i isKindOfClass:[UIPickerTable class]]) [tables addObject:i];
// etc...

Can i change the height of a uitableview's tableHeaderView?

I'm trying to change the tableView.tableHeaderView frame so i can make it bigger but with no effect. Is tableView.tableHeaderView's frame fixed or is it adjustable?
tableView.tableHeaderView = nil;
tableView.tableHeaderView = your_headerView;
See also: Resizing a UITableView's tableHeaderView
It is flexible, If you have tried following code and its still not working,
then Try changing Header View size in Interface builder.
Create a UIView with the things you want for your Header and add view
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, heightYouWant)];
tempView.backgroundColor = [UIColor whiteColor];
[yourTableView setTableHeaderView:tempView];
//[yourTableView setTableFooterView:tempView]; you can also use Footer in same way
[emptyView release];
you can add other controls in the tempView to create it run time.
Write this into your UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
return 100;
}

Add UILabel above UISearchBar and UITableView like WhatsApp Contacts

I have UISearchBar on UITableView's header, I want to add UILabel above UISearchBar. UILabel is hidden for the first time, when User scroll down the TableView, UILabel will appear. It's done in WhatsApp, in the Contacts' tab.
What is the best way to do this?? Any help will be appreciated.
Regards
One way is to add the UILabel, UISearchBar and UITableView in a UIView.
Otherwise, add the UILabel in first row, search bar in the second row and go on...
Not able to test this right now but I think:
theHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0 - HEADER_HEIGHT, 320, HEADER_HEIGHT)]
headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, HEADER_HEIGHT)];
headerLabel.textAlignment = UITextAlignmentCenter;
Also don't forget to add the subview to the header:
[theHeader addSubview:headerLabel] and to put a height for your header either within CGRectMake or defined (e.g. #define HEADER_HEIGHT 52.0f)
Let me know if it worked!

How to create a table in a view ( I dont want the table to cover up my entire screen)

Is there any way out .., that I can create a UITableView in a view such that it doesn cover up entire the screen...
For example :
I want the first half of the screen to be a UITextView and the next half portion of my screen to be
a UITableView..
Thanks in advance
CGRect cgRct = CGRectMake(0, 50, 320, 250);
UITableView *tableView1 = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
//tableView.editing = YES;
tableView1.dataSource = self;
tableView1.delegate = self;
tableView1.backgroundColor=[UIColor clearColor];
[self.view addSubview:tableView1];
You are probably creating a UITableViewController for your tableView and then realizing that you can't change the size of the tableView. Am I right? If that is the case then don't create a UITableViewController, just create a normal UIViewController and than add your tableView using Interface Builder or the code which other people posted here.
In Interface Builder (if you are using it), just drag-drop a UITableView to your UIView and adjust its width, height and y position in the Inspector.
If you are programmatically creating the view, change the frame of the UITableView
CGRect tablefrm = [tableViewObject frame];
tablefrm.origin.y = 240.0f //new origin
tablefrm.size.height = 240.0f; //new height
[tableViewObject setFrame:tablefrm]; //assuming already added the tableview to the view.

How to change the border color of a Grouped UITableView

This concerns iPhoneOS-sdk-3.2
I am having difficulty changing the border color of a grouped UITableView. I can change the cell background color, separator color, text color, quite easily now, and the rounded corners clip correctly, even when highlighted with whatever colors I have chosen. However the surrounding border remains infuriatingly gray despite many different attempts.
I have read all of the related posts I can find via Google, let alone stackoverflow. I have seen Mike Akers' heroic PITA solution for UITableViewCell clipping -- this problem is solved for iPhoneOS 3.0 and it did not help me with the border.
I have tried both a programmatic and xib-based solution and both provide the same results.
I will share the programmatic version below:
I have a UIViewController subclass rather than a UITableViewController subclass to act as a UITableView delegate -- I chose this route as I am coding on the iPad and UITableViewController reportedly takes over the whole screen. loadView method of my UIViewController subclass:
- (void) loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view release];
self.view.backgroundColor = [UIColor blackColor];
// add and configure UITableView
CGRect tableViewRect = CGRectMake(0., 0., 256., 768.);
myTableView = [[UITableView alloc] initWithFrame:tableViewRect style:UITableViewStyleGrouped];
// set the tableview delegate to this object and the datasource to the datasource which has already been set
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.sectionIndexMinimumDisplayRowCount = 1;
myTableView.backgroundColor = [UIColor clearColor];
myTableView.separatorColor = [UIColor whiteColor];
myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
myTableView.opaque = NO;
// add the table view to our background view
[self.view addSubview:myTableView];
[myTableView release];
}
I found a solution. This behavior does appear to be iPhoneOS 3.2 specific as Apple added a backgroundView property for UITableView in iPhoneOS 3.2.
I tried [myTableView.backgroundView removeFromSuperView] and UITableView just replaced it with another.
Instead, my solution was to add:
myTableView.backgroundView.hidden = YES;