iPhone - UITableViewController - Setting section header in portrait and landscape - iphone

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

Related

Button is not responsive on headerview of UITableView

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,

UIButton is not responding when added as footerview

My button is not responding to touch events, please find the code snippet as below.
I have added UIButton to UIView and set the UIView to UITableView's footerview.
Please let me know and thanks.
CGRect tblViewFrame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 300);
self.tblViewSettings = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease];
self.tblViewSettings.backgroundColor = [UIColor clearColor];
self.tblViewSettings.showsVerticalScrollIndicator = FALSE;
self.tblViewSettings.delegate = self;
self.tblViewSettings.dataSource = self;
self.tblViewSettings.scrollEnabled = YES;
// Set the background color
self.view.backgroundColor = [UIColor clearColor];
UIView *buttonView = [[[UIView alloc]initWithFrame:CGRectMake(10,10, 320, 60)]autorelease];
self.signoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.signoutButton.frame = CGRectMake(10,10, 300, 40);
UIImage *image = [[UIImage imageNamed:#"btn-large1.png"] stretchableImageWithLeftCapWidth:22 topCapHeight:0];
NSString *strSignOut = NSLocalizedString(#"Sign Out", #"SignOut Button");
[self.signoutButton setTitle:strSignOut forState:UIControlStateNormal];
[self.signoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.signoutButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
[self.signoutButton setBackgroundImage:image forState:UIControlStateNormal];
self.signoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
buttonView.backgroundColor = [UIColor clearColor];
[buttonView addSubview:self.signoutButton];
buttonView.userInteractionEnabled = YES;
//[self.view addSubview:buttonView];
self.tblViewSettings.tableFooterView = buttonView;
self.tblViewSettings.tableFooterView.userInteractionEnabled = YES;
[self.view addSubview:self.tblViewSettings];
-(IBAction)signOutBtnTapped:(id)sender{
}
I have looked at your code and applied it to my project. actually it works. i did not change anything from your code. i guess u must check out the frames of table view and the bottom view as everything else is working fine.
Try capturing the event for UIControlEventTouchDown instead of UIControlEventTouchUpInside in
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents: UIControlEventTouchUpInside];
Everything else seems OK.
for Adding button in Footer view Section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
UIButton *Login_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
[Login_Btn setImage:[UIImage imageNamed:#"btn_login.png"] forState:UIControlStateNormal];
Login_Btn.frame = kiPhoneButtonFrame;
[customView addSubview:Login_Btn];
[Login_Btn addTarget:self
action:#selector(Login_Btn_Clicked:)
forControlEvents:UIControlEventTouchUpInside];
return customView;
}
And Button Click Method
-(IBAction)Login_Btn_Clicked:(id)sender
{
// Code for button CLick which you want to do.
}

customize section header uilabel background and text

I am currently creating my own custom section headers but I have never dont any text editing via code.. and my new method that I am using to populate my custom header is doing some weird things as shown below
I would like to change the text to white and be slightly bolder and also make the white background transparent..
this is the code I am using to do this
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor grayColor]];
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.5, 20, 20)];
// do whatever headerLabel configuration you want here
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
[headerView addSubview:headerLabel];
// Return the headerView
return headerView;
}
I have tried this
[headerLabel.backgroundColor:[UIColor clearColor]];
etc but its not working :(
I would like to change the text to white...
UILabel's textColor property is your friend here.
And slightly bolder...
No problem! headerLabel.font = [UIFont boldSystemFontOfSize:mySize];
And make a white transparent background.
Whoa, whoa, that is the worst setter syntax ive ever seen!!! My lord, myLabel.backgroundColor is a getter, change:
[headerLabel.backgroundColor:[UIColor clearColor]];
to:
[headerLabel setBackgroundColor:[UIColor clearColor]];
Lucky for you, using your syntax would have just sent a message to nil, which is defaulting the background color of your label to white.
Use following code...
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headername = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 270, 34)];
headername.backgroundColor = [UIColor clearColor];
headername.textColor = [UIColor blackColor];
if(section == 0)
{
headername.text = #"Name that u wish";
}
else
{
headername.text = #"Name that u wish";
}
UIView *headerView = [[UIView alloc] init];
UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];
tempimage.image = [UIImage imageNamed:#"whitebackground.png"];
[headerView addSubview:tempimage];
[headerView addSubview:headername];
return headerView;
}
Hope, this will help you...chill
all i have done to get the customized header in table is as following and it is working fine for me
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UILabel *headerLabel =
[[[UILabel alloc]
initWithFrame:CGRectMake(10, 20, 300, 40)]
autorelease];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tableView.tableHeaderView = containerView;
i just put it in viewDidLoad: method

UITableView tableFooterView not appearing

I have a UITableView to which I add a tableFooterView, for some reason the tableFooterView is not appearing?
How I add my tableFooterView
I add the tableFooterView in a connectionDidFinishLoading method after reloading the tableview data.
So what I do is
[controls reloadData];
UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
if(self.item.canRunOnDemand)
{
UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRunWorkflow addTarget:self action:#selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
[buttonRunWorkflow setTitle:#"Run Now" forState:UIControlStateNormal];
buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44);
buttonRunWorkflow.backgroundColor = [UIColor clearColor];
[buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
canRunAlwaysLabel.text = #"Run Always:";
UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
[canRunAlways addTarget:self action:#selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
[myFooterView addSubview:canRunAlways];
[myFooterView addSubview:canRunAlwaysLabel];
[canRunAlwaysLabel release];
[canRunAlways release];
}
[myFooterView release];
[controls.tableFooterView addSubview:myFooterView];
Footer view height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 100;
}
I have also tried this:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(section == [[fields objectAtIndex:section] count] - 1)
{
return 100;
}
else {
return 0;
}
}
-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(section == [[fields objectAtIndex:section] count] - 1)
{
UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
if(self.item.canRunOnDemand)
{
UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRunWorkflow addTarget:self action:#selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
[buttonRunWorkflow setTitle:#"Run Now" forState:UIControlStateNormal];
buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44);
buttonRunWorkflow.backgroundColor = [UIColor clearColor];
[buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
canRunAlwaysLabel.text = #"Run Always:";
UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
[canRunAlways addTarget:self action:#selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
[myFooterView addSubview:canRunAlways];
[myFooterView addSubview:canRunAlwaysLabel];
[canRunAlwaysLabel release];
[canRunAlways release];
}
return myFooterView;
}
else {
return nil;
}
}
By looking at the header file of UITableView.h we see the declaration of property tableFooterView like this:
#property(nonatomic,retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer
so the default property is nil. That's why you can't add another UIView to nil UIView.
You should do something like this:
controls.tableFooterView = myFooterView;
Are you trying to add footer view in table view?
If you are trying for that Implement your footer view in this method
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
This may helps you.Thanks!!

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.