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

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;
}

Related

Objective c - proper way to set subview frame so it will fill the screen

I have a viewController inside of a navigationController, the view controller has a tableview.
In viewDidLoad I set the tableview
- (void)viewDidLoad
{
[super viewDidLoad];
// init tableView
CGRect tableFrame = self.view.bounds;
_tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
The problem with this code is that the table view frame is not correct - the height is 460 and I need it to be 416.
[The iPhone screen height is 480, minus the status bar (20) minus the navigation bar (44) = 416]
So what is the proper way to set the table view so it will fill the screen?
I can think of two ways:
set its frame to = (0, 0, 320, 416)
use: [_tableView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
Don't use magic numbers. Use the resizing flags correctly.
So yes, your 2. approach is correct.
1) Use the superviews bounds._tableView.frame = self.view.bounds;;
2) Set autoresizing flags [_tableView setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
(you did all of this already :)
Number 1 is absolutely the wrong way to do it... what happens if the screen size changes in a future OS / device?
I'm curious why you're not doing this using a nib file, and saving yourself the trouble, but if you must do it in code, set the auto-resizing mask per your option 2.

UITableView Cell at Index 0 Size

What is the best way to customize the first cell in a table to display like the Netflix application (i.e. bigger/different)? For ease of future use, I have been asked to use IB as much as possible to make it easier for future editing.
Here is a photo of the Netflix table. http://cl.ly/3j1d473d1j160502160t
Use the table header view. Drag a view to the top of the table view and it will become the "table header view".
If you have other such "cells" which are essentially constant (and might be switched on/off) you can consider putting UITableViewCell objects in your xib, and returning them from the tableView:cellForRowAtIndexPath: delegate method. Also implement the tableView:heightForRowAtIndexPath: method.
Both can have the same general outline, like:
-(UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip
{
if ( ip.section==0 ) return headerCell;
// ...handle regular cells
}
-(CGFloat)tableView:(UITableView*)tv heightForRowAtIndexPath:(NSIndexPath*)ip
{
if ( ip.section==0 ) return headerCell.frame.size.height;
// ...handle regular cells
return 44;
}
Either make it something that isn't a cell (like the table's header view), or implement -[UITableViewDelegate tableView:heightForRowAtIndexPath:] to return a different height. You can use IB to install a table header, but more complex ways of attacking this problem will require working in code.
Here is a snippet that you can use.It is table's header view...
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if(section==0){ UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.frame.size.width, 44)];
[view setBackgroundColor:[UIColor redColor]]; UILabel
*label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
[label setText:#"Hello!!"];
[label setBackgroundColor:[UIColor clearColor]];
[view addSubview:label]; return view;
}return nil;
}
Here You can set the height for the tableViewHeaderSection.
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}

Add UILabel to UITableView - 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

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 get a UITextView to autosize within a UITableViewCell

Setup: I have a UITextView inside a UITableViewCell's contentView. I want it to take up the full size of the cell. I create the text view like so:
UITextView *textView = [[[UITextView alloc] initWithFrame:CGRectMake(0,0,268,43)] autorelease];
textView.backgroundColor = [UIColor redColor];
textView.layer.cornerRadius = 10;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
And I override heightForRowAtIndexPath to return 200 for that row.
The background color is just so I can tell where it is. The cell seems to be automatically sizing it correctly upon first view. However, I need it to continue to resize it correctly while autorotating the interface, which only seems to work sometimes, and only when I'm not editing the textView. Other times, it resizes the view to have a very small height (looks like -1), or makes it too wide, or just doesn't even resize it at all.
I've tried overriding layoutSubviews in the cell and just do nothing, but even that doesn't stop the view from resizing all over the place.
I've been hacking away at this for awhile now, but still have found no solution.
A UITableViewCell has a fixed height, the height provided by the UITableView's delegate. When you rotate your device, the height of the row will never change, unless you call -reloadData on your tableView. I'd get rid of the autoresizing and manage it yourself.
When you init your textField, you can easily set the frame to CGRectZero. Then implement -layoutSubviews (and call super in that method, before setting the frames of your subviews) and set the frame of the UITextField according to the contentRect property of the cell.
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super ...]){ // or -initWithStyle:reuseIdentifier: whatever you want
_textView = [[UITextView alloc] initWithFrame:CGRectZero]; // Instance variable
// Probably not needed to set autoresizing mask
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_textView.frame = CGRectMake(0.0f, 0.0f, self.contentRect.size.width, self.contentRect.size.height); // Adjust as needed
}