UITableView tableFooterView not appearing - iphone

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!!

Related

Created Table Cell With UIView as subview Not Displaying in iOS7

I have created UIView on top of the UITable Cell but its not displaying in iOS7 which is working fine in iOS6. Kindly provide me the solution.
We are adding the More button # the end of the table.
Added code for the same:
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 40)];
footerView.backgroundColor = [UIColor grayColor];
UIButton *btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
btnLoadMore.frame = CGRectMake(-10, 0, 768, 30);
btnLoadMore.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
[btnLoadMore setTitle:#"Sample" forState:UIControlStateNormal];
[btnLoadMore setBackgroundColor:[UIColor redColor]];
[btnLoadMore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLoadMore addTarget:self action:#selector(loadMore) forControlEvents:UIControlEventTouchUpInside];
btnLoadMore.userInteractionEnabled=YES;
[btnLoadMore.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:17.0]];
[footerView addSubview:btnLoadMore];
[footerView setHidden:YES];
[footerView setTag:999];
[cell addSubview: footerView];
for (id subView in [cell subviews]) {
[subView setHidden:NO];
}
UIView *lastRow = [cell viewWithTag:999];
[lastRow setHidden:YES];
if(indexPath.section == [arSearch count] && isLoadMore){
for (id subView in [cell subviews]) {
[subView setHidden:YES];
}
cell.backgroundView = nil;
[lastRow setHidden:NO];
Check your code. Why you have written following line :
[footerView setHidden:YES];
I think you are hiding view from cell.
Instead of
[cell addSubview: footerView];
use
[cell.contentView addSubview: footerView];
Also remove
[footerView setHidden:YES];
For loadmore, I always do below.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
v.backgroundColor = [UIColor clearColor];
int mySiz = 0;
// keep counter how many times load more is pressed.. initial is 0 (this is like index)
mySiz = [startNumberLabel.text intValue]+1;
// i have 15 bcz my index size is 15.
if ([feeds count]>=(15*mySiz)) {
NSLog(#"showing button...");
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10, 10, 296, 45)];
[button setBackgroundImage:[UIImage imageNamed:localize(#"loadmore")] forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadMoreData:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:button];
mainTableView.tableFooterView = v;
} else {
mainTableView.tableFooterView = nil;
}
}
[mainTableView reloadData];

hide custom header in tableview in iphone

I am using custom header in category tableview. So, in accordance to that condition, I have to hide header and it's detail. So, I used following code for displaying header with condition.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
if(section==0)
{
if(![appDelegate.purchase_requisitions_preference isEqualToString:#"0"] && ![appDelegate.purchase_requisitions_preference isEqualToString:#"null"])
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 320, 30)];
btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f];
[btn setTag:section+1];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(20, 0, 320, 30);
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = #"Purchase Order Requisitions";
[label1 setTag:section+1];
[btn addSubview:label1];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
}
else
{
return nil;
}
}
else if(section==1)
{
if(![appDelegate.hr_vacancies_preference isEqualToString:#"0"] && ![appDelegate.hr_vacancies_preference isEqualToString:#"null"])
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 320, 30)];
btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f];
[btn setTag:section+1];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(20, 0, 320, 30);
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = #"Human Resource Vacancies";
[label1 setTag:section+1];
[btn addSubview:label1];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
}
else
{
return nil;
}
}
else if(section==2)
{
if(![appDelegate.hr_offers_preference isEqualToString:#"0"] && ![appDelegate.hr_offers_preference isEqualToString:#"null"])
{
NSLog(#"in offer");
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 320, 30)];
btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f];
[btn setTag:section+1];
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(20, 0, 320, 30);
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor whiteColor];
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = #"Human Resource Job Offers";
[label1 setTag:section+1];
[btn addSubview:label1];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
}
else
{
return nil;
}
}
return aView;
}
If my condition is false, then I want to hide my header as well as it's detail. Its detail is getting hide but header is still displaying there.
return 0 in heightForHeaderInSection: method if your Data is nil otherwise return 30( just example) like bellow..
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section==0)
{
if(![appDelegate.purchase_requisitions_preference isEqualToString:#"0"] && ![appDelegate.purchase_requisitions_preference isEqualToString:#"null"])
{
return 30;// any height which you want
}
else{
return 0;
}
}
// and so on
}
you need to set Height of Header Section,
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section==0 | section==1 |section==2)
return 30;
else
return 0;
}

iPhone-Grouped Table View adding two radio buttons for each row

I want to display two radio buttons in each row and i need to set action for two buttons i was facing problem in setting the actions and display text and radio buttons in correct position for each row, If some one help please.
Here is my code
Thanks in advance
.h file
#import <UIKit/UIKit.h>
//#class CustomCellQuestionnaireList;
#interface QuestionnaireListView : UIViewController <UITableViewDataSource, UITableViewDelegate,UINavigationControllerDelegate,UISearchBarDelegate>
{
//NSMutableArray *dataArray;
UITableView *theTableView;
UILabel *lbl_child;
UILabel *lbl_title;
UILabel *lbl_time;
NSMutableArray *tableData;//will be storing data that will be displayed in table
NSMutableArray *tableSubData;
//NSMutableArray *tableTitleArray;
//CustomCellQuestionnaireList *cell;
IBOutlet UIButton *myButton;
IBOutlet UIButton *myButton2;
BOOL isSelected;
IBOutlet UIView *view1;
IBOutlet UIBarButtonItem *saveBtn;
IBOutlet UIBarButtonItem *discardBtn;
IBOutlet UIBarButtonItem *okBtn;
//IBOutlet UIToolbar *m_toolBar;
//IBOutlet UIToolbar *m_okToolBar;
}
#property (nonatomic,retain) IBOutlet UILabel *lbl_child;
#property (nonatomic,retain) IBOutlet UILabel *lbl_title;
#property (nonatomic,retain) IBOutlet UILabel *lbl_time;
#property (nonatomic,retain) IBOutlet UITableView * theTableView;
//#property(nonatomic,retain) NSMutableArray *dataArray;
#property(nonatomic, retain) NSMutableArray *tableData;
#property(nonatomic, retain)NSMutableArray *tableSubData;
#property(nonatomic, retain)UIView *view1;
#property(nonatomic, retain)UIBarButtonItem *saveBtn;
#property(nonatomic, retain)UIBarButtonItem *discardBtn;
#property(nonatomic, retain)UIBarButtonItem *okBtn;
//#property(nonatomic, retain)UIToolbar *m_toolBar;
//#property(nonatomic, retain)UIToolbar *m_okToolBar;
- (IBAction)onClickLeftArrow;
- (IBAction)onClickRightArrow;
//- (IBAction)savePressed:(id)sender;
- (IBAction)selectRadioButon:(UIButton *)button;
//-(IBAction)okPressed:(id)sender;
#end
.m file
#implementation QuestionnaireListView
#synthesize tableData, tableSubData;
#synthesize theTableView;
//#synthesize dataArray;
#synthesize lbl_child;
#synthesize lbl_title;
#synthesize lbl_time;
#synthesize view1;
#synthesize saveBtn;
#synthesize discardBtn;
#synthesize okBtn;
//#synthesize m_toolBar;
//#synthesize m_okToolBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"saveBtn.png"] forState:UIControlStateNormal];
[button1 setFrame:CGRectMake(0, 0, 66, 34)];
[button1 addTarget:self action:#selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Save" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease];
[toolBarItems addObject:barButtonItem1];
//[self setToolbarItems:toolBarItems];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"okBtn.png"] forState:UIControlStateNormal];
[button2 setFrame:CGRectMake(100, 0, 66, 34)];
//[button2 addTarget:self action:#selector(home:) forControlEvents:UIControlEventTouchUpInside];
//[button2 setTitle:#"Home" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
[toolBarItems addObject:barButtonItem2];
button2.hidden = YES;
//[self setToolbarItems:toolBarItems];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setImage:[UIImage imageNamed:#"okBtn.png"] forState:UIControlStateNormal];
[button3 setFrame:CGRectMake(150, 0, 66, 34)];
//[button4 addTarget:self action:#selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
//[button4 setTitle:#"Home" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button3];
[toolBarItems addObject:barButtonItem3];
button3.hidden = YES;
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setImage:[UIImage imageNamed:#"discardBtn.png"] forState:UIControlStateNormal];
[button4 setFrame:CGRectMake(200, 0, 66, 34)];
[button4 addTarget:self action:#selector(discardBtn:) forControlEvents:UIControlEventTouchUpInside];
[button4 setTitle:#"Discard" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithCustomView:button4];
[toolBarItems addObject:barButtonItem4];
[self setToolbarItems:toolBarItems];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//[self.theTableView.layer setCornerRadius:30.0];
// create button
//UIButton* logOutButton = [UIButton buttonWithType:101]; // left-pointing shape!
UIButton *logOutButton = [UIButton buttonWithType:100];
[logOutButton setImage:[UIImage imageNamed:#"exitBtn.png"] forState:UIControlStateNormal];
[logOutButton addTarget:self action:#selector(logoutButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[logOutButton setTitle:#"Logout" forState:UIControlStateNormal];
UIBarButtonItem* logOutItem = [[UIBarButtonItem alloc] initWithCustomView:logOutButton];
self.navigationItem.leftBarButtonItem = logOutItem;
[lbl_title setText:#"Store View"];
[lbl_title setTextColor:[UIColor whiteColor]];
//NSString *date = [[NSDate date] dateFormat];
NSString *date = [NSDate date];
NSLog(#"date:%#",date);
[lbl_time setText:[NSString stringWithFormat:#"%#",date]];
[lbl_time setTextColor:[UIColor whiteColor]];
// add to toolbar, or to a navbar (you should only have one of these!)
//[toolbar setItems:[NSArray arrayWithObject:backItem]];
//tableTitleArray = [[NSMutableArray alloc]init];
// [tableTitleArray addObject:#"General"];
// [tableTitleArray addObject:#"Product"];
tableData = [[NSMutableArray alloc] init];
[tableData addObject:#"Is the floor clean?"];
[tableData addObject:#"Are the food items stacked in freezer?"];
tableSubData = [[NSMutableArray alloc] init];
[tableData addObject:#"Has the snow been cleared at the entrance?"];
[tableData addObject:#"Are milk products available"];
view1.hidden = YES;
//m_okToolBar.hidden= YES;
//m_toolBar.barStyle = UIBarStyleBlackTranslucent;
//m_toolBar.tintColor = [UIColor blackColor]; m_toolBar.alpha = 0.7;
//UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"nextBtn.png" style:UIBarButtonItemStyleBordered target:self action:#selector(customButtonHandler:)]];
// btn.frame = CGRectMake(5, 20, 30, 30);
//[btn release];
}
-(void)saveBtn:(id)sender {
NSLog(#"Button pressed");
view1.hidden = NO;
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"saveBtn.png"] forState:UIControlStateNormal];
[button1 setFrame:CGRectMake(0, 0, 105, 34)];
//[button1 addTarget:self action:#selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
//[button1 setTitle:#"Save" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease];
[toolBarItems addObject:barButtonItem1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"okBtn.png"] forState:UIControlStateNormal];
[button2 setFrame:CGRectMake(0, 0, 66, 34)];
[button2 addTarget:self action:#selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"OK" forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
[toolBarItems addObject:barButtonItem2];
[self setToolbarItems:toolBarItems];
button1.hidden = YES;
button2.hidden = NO;
button3.hidden = YES;
button4.hidden = YES;
}
-(void)okBtn:(id)sender {
view1.hidden = YES;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:3] animated:YES];
}
//-(void)viewWillAppear:(BOOL)animated
//{
//
// //VizueraQAppDelegate *appDelegate = (VizueraQAppDelegate *)[[UIApplication sharedApplication] delegate];
// //[appDelegate RefreshIndexArray];
//
//}
- (void)logoutButtonTouched {
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
- (IBAction)onClickLeftArrow {
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)onClickRightArrow {
//[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// release the array
// self.dataArray = nil;
// tableData = nil;
}
- (void)dealloc
{
//[cell release];
[tableData release];
[tableSubData release];
[super dealloc];
}
#pragma mark - UIViewController delegate methods
- (void)didReceiveMemoryWarning
{
// invoke super's implementation to do the Right Thing, but also release the input controller since we can do that
// In practice this is unlikely to be used in this application, and it would be of little benefit,
// but the principle is the important thing.
//
[super didReceiveMemoryWarning];
}
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
// return YES;
//}
#pragma mark - UITableView delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableData!=nil && tableSubData!=nil) {
return 2;
}
else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [tableData count];
}
else {
return [tableSubData count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 74;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//return ([NSString stringWithFormat:#"%#", [tableTitleArray objectAtIndex:section]]);
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *returnString = #"";
if (tableData!=nil && section == 0) {
returnString = #"General";
}
else if(section == 1){
returnString = #"Product";
}
return returnString;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = #"MyCellID";
UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:kCustomCellID];
//CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kCustomCellID] autorelease];
//cell = (CustomCell *)[[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCustomCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag=[indexPath row];
NSInteger tagCount;
myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 48, 20, 20)];
[myButton setImage:[UIImage imageNamed:#"radioBtn.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = ++tagCount;
[cell.contentView addSubview:myButton];
tagCount++;
myButton2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 48, 20, 20)];
[myButton2 setImage:[UIImage imageNamed:#"radioBtn.png"] forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton2.tag = tagCount;
[cell.contentView addSubview:myButton2];
tagCount++;
/*UILabel *m_label = [[UILabel alloc] init];
m_label.text = #"Yes";
m_label.textColor = [UIColor redColor];
m_label.frame = CGRectMake(15, 48, 40, 40);
[m_label release]; */
CGRect labelRect = CGRectMake(35, 49, 30, 15);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
//label.backgroundColor = [UIColor redColor];
label.text = #"Yes";
label.textAlignment = UITextAlignmentLeft;
label.font = [UIFont boldSystemFontOfSize:14];
label.tag = 10;
[cell.contentView addSubview:label];
[label release];
CGRect labelRect1 = CGRectMake(125, 50, 30, 15);
UILabel *label1 = [[UILabel alloc] initWithFrame:labelRect1];
//label.backgroundColor = [UIColor redColor];
label1.text = #"No";
label1.textAlignment = UITextAlignmentLeft;
label1.font = [UIFont boldSystemFontOfSize:14];
label1.tag = 10;
[cell.contentView addSubview:label1];
[label1 release];
}
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if([tableData count]>indexPath.row)
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.textLabel.frame = CGRectMake(0, 0, 20, 20);
return cell;
}
- (IBAction)selectRadioButon:(UIButton *)button{
for (UIButton *but in [self.view subviews]) {
if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
[but setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
if (isSelected) {
[button setImage:[UIImage imageNamed:#"radioBtn.png"] forState:UIControlStateNormal];
isSelected = NO;
}
else {
[button setImage:[UIImage imageNamed:#"radioBtn_active.png"] forState:UIControlStateNormal];
isSelected = YES;
}
}
Keep your "initWithFrame" codes inside the if(cell == nil) { } for when you're drawing your table cells and move all the "setting" UI elements outside that if.
See my code example here:
Can't Change Transparent BG in Custom UITableViewCell (Grouped Table View)

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.