Created Table Cell With UIView as subview Not Displaying in iOS7 - iphone

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

Related

how to set the scroll view contentsize dynamically?

I had an application in which i am setting the content size of the scroll view dynamically according to the number of buttons which i am creating from an array.like this `
for(int i =0;i<[sarray count];i++)
{
NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
NSString *head=[dicttable objectForKey:#"cat"];
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*87,0,87, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[Scroller addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
if(btn.tag==0)
{
sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
{
btn.selected=NO;
}
}
}
[Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];
`
but here the problem is after the last button also the scroll view scrolls .it need not to be happend,i need the scrolling ends with last button on the view,can anybody help me on this
set scrollview bounces to NO
scrollView.bounces = NO;
yes as you said it looses the smoothness. But no other choice. If you found any sol please update the answer.
hey i am sending you complete example to make button dynamicay and seting the content size scrollview on number of count ,i had just given u one example code u have to set the size of frame of button as u requried
UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];
int y=0;
for(int i =1;i<10;i++)
{
// NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
// NSString *head=[dicttable objectForKey:#"cat"];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(y,89,100, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
NSString *str=[NSString stringWithFormat:#"%d",i];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:str forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[scroll_View addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
y=110*i;
}
[scroll_View setContentSize:CGSizeMake(y, 400)];
enjoy coding
You just declare one yMargin variable of type CGFloat and initialize it with some required value as 0. Now every time whenever you are adding a new subview vertically do y+=lbl.frame.size.height;
Now setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];
(someRequiredMargin - you want to add below )

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

Place custom view on Navigation Bar

I want to display Image,Title name,and two left right buttons. That I have done successfully.
Here is the code:
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
UIButton *leftButton = [[UIButton alloc]init];
[leftButton setImage:[UIImage imageNamed:#"previous-arrow.png"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(goPrevious:) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(20, 50, 14, 13)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Title.png"]];
[imageView setFrame:CGRectMake(leftButton.frame.origin.x+leftButton.frame.size.width+4, 20, 50, 50)];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 30, 100, 40)];
NSString *cName = [NSString stringWithFormat:#"%#",[NameArray objectAtIndex:indexPath.section]];
[nameLabel setTextColor:[UIColor whiteColor]];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setContentMode:UIViewContentModeScaleAspectFill];
[nameLabel setTextAlignment:UITextAlignmentLeft];
CGSize expectedLabelSize = [cName sizeWithFont:nameLabel.font
constrainedToSize:CGSizeMake(100, 40)
lineBreakMode:nameLabel.lineBreakMode];
[nameLabel setFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 35, expectedLabelSize.width, expectedLabelSize.height)];
nameLabel.text=cName;
UIButton *rightButton = [[UIButton alloc]init];
[rightButton setImage:[UIImage imageNamed:#"next-arrow.png"] forState:UIControlStateNormal];
[rightButton setFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+4, 50, 14, 13)];
[rightButton addTarget:self action:#selector(next:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:nameLabel];
[view addSubview:imageView];
[view addSubview:leftButton];
[view addSubview:rightButton];
self.navigationItem.titleView = view;
This code is written in cellForRowAtIndexPath. The view is showing properly on navigation bar but after scrolling the tableview. Initially it is displaying on wrong coordinates.
Please Suggest.
Put this code in viewWillAppear or viewDidAppear. It's not meant to be in tableView protocol!

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

UIButton in TableViewCell not responding

if ([cell.textLabel.text isEqualToString:#"Button"])
{
UIView *ButtonView = [[UIView alloc] initWithFrame:CGRectMake(100, 5, 100, 25)];
UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Button.frame = CGRectMake(90, 5, 90, 30);
[Button setTitle:#"PopOver" forState:UIControlStateNormal];
[ButtonView addSubview:Button];
[cell.contentView addSubview:ButtonView];
[Button addTarget:self action:#selector(showPickerPopupAction)forControlEvents:UIControlEventTouchUpInside];
Button.tag = indexPath.row;
}
- (void) showPickerPopupAction
{
NSLog( #"Button clicked." );
}
The Button's frame is almost completely outside the ButtonView's frame. If you set a backgroundColor on the ButtonView, you'll see that only the top-left corner of the button is in the ButtonView frame and only that corner will respond to touches.
Adjust the size of the ButtonView frame or the size and position of the Button frame so that the button is completely inside ButtonView.
Not sure why you need ButtonView in the first place. You could just add Button directly to the contentView.
Also, you should do [ButtonView release] after adding ButtonView to the cell.contentView.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ButtonCell"] autorelease];
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
cell.backgroundView = backgroundView;
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonLeft setTitle:#"Left" forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 10.0f, 3.0f, 145.0f, 40.0f)];
[buttonLeft addTarget:self action:#selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRight setTitle:#"Right" forState:UIControlStateNormal];
[buttonRight setFrame: CGRectMake( 165.0f, 3.0f, 145.0f, 40.0f)];
[buttonRight addTarget:self action:#selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:buttonLeft];
[cell addSubview:buttonRight];
[backgroundView release];
-(void) addToFavorites {}
Works fine !!