I am making an iPhone app. In this app I have a UITableView which has a UITableView header. This is how my code looks like.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 308, 50)];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
self.tableViewAppointments.tableHeaderView = view;
And this is how it looks like on my iPhone
And this is what I want to achieve
Hope that anyone can help me !
Kind regards !
Because you're specifying explicitly the frame of the view. You should not do a terrible hack like this. Use this piece of code instead:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]];
self.tableView.tableHeaderView = imgView;
If you want to add it as a table view section header, you need to add it as follows,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 308, 50)];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
//or,
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
return view;
}
You can define the height for header in
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return height;
}
Related
I am creating headers for my tableview.And adding view on that header.
In that view there are two imageviews and a button
The button is not responsive(a very small area on the top part is responsive)
Why this is happening,Please help me ?
I have tried with some SO answers- like "setAutoresizingMask:UIViewAutoresizingNone"
But nothing is working
Here is my code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// ADDING A VIEW
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 39.0)];
header.backgroundColor = [UIColor clearColor];
header.userInteractionEnabled = YES;
[header setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
// ADDING IMAGE VIEW
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 310, 39.0)];
image.image = [UIImage imageNamed:#"headertop.png"];
[image setUserInteractionEnabled:TRUE];
[header addSubview:image];
// ADDING ANOTHER IMAGEVIEW (beneath the view's frame)
UIImageView *downImage = [[UIImageView alloc]initWithFrame:CGRectMake(0 , 39, 310, 23)];
downImage.image = [UIImage imageNamed:#"headerbottom.png"];
[header addSubview:downImage];
// ADDING BUTTON
UIButton *placeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[placeBtn addTarget:self
action:#selector(goToRestPage:)
forControlEvents:UIControlEventTouchUpInside];
placeBtn.tag = section;
placeBtn.backgroundColor = [UIColor yellowColor];
placeBtn.frame = CGRectMake(75,20, 150, 20);
[header addSubview:placeBtn];
return header;
}
Use this Method. I think you miss this method.
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
Or this method already exists so increase the height of the header.
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 80.0;
}
Please do this :
User Interaction Enabled to TRUE from your XIB. by default its FALSE.
Thanks,
Hi all I am developing an app.I want to show a table view with a header view.When i navigate from Root view to Next view(tableview) which is as shown below.But my question is when i passes some text from root view to Next view(table view) as a header to next view it shows like the following in appropriate format.i need solution for this to show some better format.Following code that i used for this.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* NSString *str_header1;
str_header1=[[NSString alloc]initWithString:#"Heading : "];
str_header1=[str_header1 stringByAppendingFormat:str_code1];
str_header1=[str_header1 stringByAppendingFormat:#" - "];
str_header1=[str_header1 stringByAppendingFormat:str_header]; */
// Create label with section title
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 300, 20)];
UILabel *titleLabel = tmpTitleLabel;
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.numberOfLines = 0;
titleLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
titleLabel.shadowColor = [UIColor whiteColor];
titleLabel.shadowOffset = CGSizeMake(0.0, 1.0);
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.text = str_combine;
//Calculate the expected size based on the font and linebreak mode of label
CGSize maximumLabelSize = CGSizeMake(300,9999);
expectedLabelSize= [str_combine sizeWithFont:titleLabel.font constrainedToSize:maximumLabelSize lineBreakMode:titleLabel.lineBreakMode];
//Adjust the label the the new height
CGRect newFrame = titleLabel.frame;
newFrame.size.height = expectedLabelSize.height;
titleLabel.frame = newFrame;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 320, expectedLabelSize.height+30)];
[view addSubview:titleLabel];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section
{
return expectedLabelSize.height+30;
}
Your code assumes that -tableView:viewForHeaderInSection: is always called before -tableView:heightForHeaderInSection: but that is not the case. You could move the size calculation to -tableView:heightForHeaderInSection: to fix that.
Even better, as you seem to use only one table header view: There is a property on UITableView called tableHeaderView which you can use. Rename your method - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section to -(UIView *)headerView, delete the method -tableView:heightForHeaderInSection: and add the following line at the end of the view controller's -viewDidLoad method:
self.tableView.tableHeaderView = [self headerView];
I need a UITableView whose first row should show a logo, while other rows should show the other table datas.
I tried the following code:
if(indexPath.row == 0) {
//Code for imageview
} else {
//Code to display array content
}
My problem is that I don't get the first array item in the table view.
Edited::
if (indexPath.row == 0) {
CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"logo.png"]];
[cell.contentView addSubview:myImage];
}
else {
NSDictionary *dict = [menuitems objectAtIndex:indexPath.row];
cell.textLabel.text =[dict objectForKey:#"category"];
}
Try to use an headerview. Use the following code to place logo at the top of row.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UIImageView *myimgvw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[myimgvw setImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:myimgvw];
return myView;
}
You haven't shown your code that displays the array content, but I guess you are using something like
[dataArray objectAtIndex:indexPath.row];
But because you are showing the image at row 0, you need to offset this by -1. In other words, the second row in your table will have indexPath.row = 1, but you need that to be objectAtIndex:0.
Then you need to put use indexPath-1 to show text instead of indexPath in else part of above code snippet.
I want to add subview in UITableView
I tried like this way:
[self.tableView addSubView:myView.view];
I didn't got any error but view is not getting added, can anyone please help..?
Thanks.
Try this:
view.frame = CGRectMake(view.frame.origin.x, -view.frame.size.height, view.frame.size.width, view.frame.size.height);
[self.tableView addSubview:view];
[self.tableView setContentInset:UIEdgeInsetsMake(view.frame.size.height, 0, 0, 0)];
this will add the view above the cells and move the cells down by the height of the added view.
I am no getting why you are adding subview to Tableview. May be you want to add something to each cell of tableView. You can do this by adding:
[cell.contentView addSubview:yourSubView];
in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Both of self.tableView and myView should not be nil
What do you mean myView.view?
this piece of code works good for me:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 200, 20)];
labelView.text = #"TEST";
[headerView addSubview:labelView];
[self.tableView addSubview:headerView];
For Swift:
let foregroundView = UILabel(frame: CGRectMake(0, 0, tableView.bounds.size.width, tableView.bounds.size.height))
foregroundView.backgroundColor = UIColor.whiteColor()
tableView.addSubview(foregroundView)
foregroundView.bringSubviewToFront(tableView)
You can remove it also:
foregroundView.removeFromSuperview()
Simply replace your line of code with the one below:
[self.tableView.superview addSubview:myView];
Note:
I am not certain about myView.view in line of code you mentioned in your question.
A quick question, for a quick answer (since I'm not finding any):
Is there a way to change the font of the section's title (given by titleForSection) in iPhone?
Thanks a lot!
Thanks Jasarien! You were absolutely right.
I leave my code here to help someone with the same problem:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = #"Just a title";
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0, 284, 23);
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:#"Helvetica" size:14];
label.text = sectionTitle;
label.backgroundColor = [UIColor clearColor];
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[view autorelease];
[view addSubview:label];
return view;
}
You'll have to use the viewForHeaderInSection: method and provide your own view. Luckily this can be a UILabel with a specified font, so you can do it quite easily.
You have to override
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
to give required height for the section title. Else it will overlap with the cell.
viewForHeaderInSection might work fine... but here's an alternative that works fine for me (Xcode 5 and IOS7 target):
// To set the background color and text color of the Table Headers
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000];
// Text Color & Alignment
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
[header.textLabel setTextAlignment:NSTextAlignmentCenter];
// Text Font
UIFont *saveFont = header.textLabel.font;
[header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]];
// Another way to set the background color
// Note: does not preserve gradient effect of original heade!r
// header.contentView.backgroundColor = [UIColor blackColor];
}
Here's what it looks like