Button is not responsive on headerview of UITableView - iphone

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,

Related

UITableView headerview keeps repeating

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

Blue coloured Footer in UITableView for an iPhone Native App in Objective C

I am developing an iPhone App using X-Code in objective C. I want to add a blue coloured footer (same as header) at the bottom of the table.
Can anyone help me in suggesting How this can be achieved. Also How to add a button in this footer.
Thanks in advance.
Hope this will help you
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *viewFotter = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewFotter setBackgroundColor:[UIColor blueColor]];
return viewFotter;
}
But the thing is, u can't resize the button.Because it's doesn't worked for me.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
footerView.backgroundColor =[UIColor blueColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"BUTTON" forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 5, 100, 30 );
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:btn];
return footerView;
}
-(void) btnClicked:(id) sender{
//ur code
}
paste this code in your .m file also you can add image in your footerview
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *viewFotter = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewFotter setBackgroundColor:[UIColor blueColor]];
UIButton *GoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[GoBtn setTitle:#"GO" forState:UIControlStateNormal];
GoBtn.frame = CGRectMake(85, 7, 150, 39 );
[GoBtn addTarget:self action:#selector(GOButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[viewFotter addSubview:GoBtn];
return viewFotter;
}
You can add the button by just dragging the UIButton to the footer of the UITableView in xib.But you can't resize the button as it will automatically assigns to the table view's width.
But I don't understand what you are saying regarding footer color.Can you pls elaborate it.
You can create a custom view with the components you want (UIButton, UIImageView, etc) and set the required values in
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Or you could just assign the same view to tableFooterView
tableView.tableFooterView = yourBlueView;

Dynamically Loading Tableview Header with Calculated size

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

iPhone - UITableViewController - Setting section header in portrait and landscape

I'm having UITableView controller which uses custom section header view. When my device is portrait, its showing perfect. And when my device is landscape its perfect again. But when orientation changed during app runtime, its not updating my section view. What's the problem?
Using following code, I'm adding a label, and two buttons to a view and returning that view in "viewForHeaderInSection". During debugging I found that the method is getting called, but the section header view is not updating.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSDictionary* dict = [threadsArray objectAtIndex:section];
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
headerView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.2];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, self.view.frame.size.width - 98, 21)];
newLabel.textColor = [UIColor blackColor];
newLabel.font = [UIFont boldSystemFontOfSize:17];
newLabel.text = [dict objectForKey:#"Name"];
newLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:newLabel];
UIButton* addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
addButton.frame = CGRectMake(self.view.frame.size.width - 88, 11, 29, 29);
NSLog(#"%f %f %f %f",addButton.frame.origin.x,addButton.frame.origin.y, addButton.frame.size.width,addButton.frame.size.height);
[addButton addTarget:self action:#selector(addThreadResponseClicked:) forControlEvents:UIControlEventTouchDown];
[headerView addSubview:addButton];
UIButton* expandCollapseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[expandCollapseButton setImage:[UIImage imageNamed:#"Expand.png"] forState:UIControlStateNormal];
expandCollapseButton.frame = CGRectMake(self.view.frame.size.width - 48, 11, 29, 29);
NSLog(#"%f %f %f %f",expandCollapseButton.frame.origin.x,expandCollapseButton.frame.origin.y, expandCollapseButton.frame.size.width,expandCollapseButton.frame.size.height);
[expandCollapseButton addTarget:self action:#selector(expandCollapseHeader:) forControlEvents:UIControlEventTouchDown];
[headerView addSubview:expandCollapseButton];
addButton.tag = expandCollapseButton.tag = section;
return [headerView autorelease];
}
Try setting the views autoresizing masks appropriately: eg for headerView;
[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
and for addButton and expandCollapseButton set the AutoresizingMask to:
UIViewAutoresizingMaskFlexibleLeftMargin

UIButton in UIView in UITableView = can't click the button

If I add the button straight everything goes fine. I'd really like to add it inside a view and still be able to click it. Here's the code:
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
footerButton.frame = CGRectMake(10, 10, 300, 40);
[footerButton addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:footerButton];
Did you set the footer height to the height of the view you are adding to it? I had a similar problem when I returned a UIView containing a UIButton in the method viewForFooterInSection. The footer height was not large enough until I updated the heightForFooterInSection method. For example:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return containerView.bounds.size.height;
}
All works fine! In your TableViewController do:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
footerButton.frame = CGRectMake(10, 10, 300, 40);
[footerButton addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:footerButton];
[footerButton setTitle:#"Touch me!" forState:UIControlStateNormal];
containerView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
self.tableView.tableFooterView = containerView;
}
- (void)click {
NSLog(#"I am here!");
}
This #selector(click) looks a little odd. I would have thought your click function would like something like:
- (void)click:(id)sender;
If that's what you have then you need to add the colon #selector(click:) to make the signatures match.