Can't Able to access the Header View in Iphone - ios

I added a header view in the TableView, but I can't able to access that view or any subview of Header view in the IBAction method.
My Code is as follow:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header_View=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)];
header_View.tag=section;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.Frame=header_View.bounds;
[btn setTitle:[arr_headerTitles objectAtIndex:section] forState:UIControlStateNormal];
btn.titleLabel.font=FontChampagneLimousinesBold(18);
btn.tag=section;
[btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *img_arrow=[[UIImageView alloc]initWithFrame:CGRectMake(150, 05, 15, 15)];
img_arrow.image= [UIImage imageNamed:#"arrow_white_down.png"];
[header_View addSubview:btn];
[header_View addSubview:img_arrow];
return header_View;
}
-(void)expandTableHeaderView:(UIButton*)sender{
// UIView* _header_view =(UIView*)[_tbl viewWithTag:[sender tag]]; //Also tried
UIView *_header_view=[_tbl headerViewForSection:section]; //shows nil;
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]];
}
Help me to solve this
*Thank you*

Try using UITableViewHeaderFooterView instead of UIView while creating the header_View, seems working
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeaderFooterView *header_View=[[UITableViewHeaderFooterView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)];
header_View.tag=section;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.Frame=header_View.bounds;
[btn setTitle:[arr_headerTitles objectAtIndex:section] forState:UIControlStateNormal];
btn.titleLabel.font=FontChampagneLimousinesBold(18);
btn.tag=section;
[btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *img_arrow=[[UIImageView alloc]initWithFrame:CGRectMake(150, 05, 15, 15)];
img_arrow.image= [UIImage imageNamed:#"arrow_white_down.png"];
[header_View addSubview:btn];
[header_View addSubview:img_arrow];
return header_View;
}
-(void)expandTableHeaderView:(UIButton*)sender{
// UIView* _header_view =(UIView*)[_tbl viewWithTag:[sender tag]]; //Also tried
UITableViewHeaderFooterView *_header_view=[_tbl headerViewForSection:sender.tag]; //shows nil;
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]];
}

try this:
-(void)expandTableHeaderView:(UIButton*)sender{
UIView *_header_view = sender.superview;
// ...
}

I think your header height is not defined.
Try this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40.0;//the height of your header view
}
This might work.
And please put breakpoint marker in your IBAction method to see if its working or not.

Related

How Can I Change TableView Header Button Image On Click. Need code in Objective c

This is my code, just added the image but not changing when hit the button.
Please suggest a working code.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
const CGRect fr = CGRectMake(0, 0, 320.0, 40.0 );
btn = [[UIButton alloc]initWithFrame:fr];
UIImageView *imgArrow = [[UIImageView alloc]initWithFrame:CGRectMake(380, 2.5, 25, 25)];
imgArrow.image=[UIImage imageNamed:#"arrow.jpeg"];
[btn setTitle:[self tableView:tableView titleForHeaderInSection:section] forState:UIControlStateNormal ];
btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[btn setBackgroundColor:[UIColor redColor]];
[btn setTag:section];
[btn addSubview:imgArrow];
[btn addTarget:self action:#selector(sectionOpenToggle:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
First, buttons already have an image view that you can use, so don't do this:
[btn addSubview:imgArrow];
Instead, do this:
[btn setImage:[UIImage imageNamed:#"arrow.jpeg"] forState:UIControlStateNormal];
Then, in your sectionOpenToggle method (assuming it looks like this), change the image:
- (void)sectionOpenToggle:(UIButton *)sender {
[sender setImage:[UIImage imageNamed:#"other-arrow.jpeg"] forState:UIControlStateNormal];
// whatever else you want to do
}

iOS: How to add swipe to delete button to a custom view in viewForHeaderInSection

There is a expand-collapse like table view with custom header using viewForHeaderInSection.
In that, I want to add swipe gesture functionality to delete the same view i.e section header.
My code for viewForHeaderInSection is,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
mView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 110)]autorelease];
mView.backgroundColor=[UIColor whiteColor];
[mView setBackgroundColor:[UIColor whiteColor]];
UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 28)];
title.text=[[updateDataArray objectAtIndex:section] objectForKey:#"message"];
title.font = [UIFont fontWithName:#"Raleway-Medium" size:18];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 320, 44)];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[bt setTag:section];
addCellFlag=2;
[bt.titleLabel setFont:[UIFont systemFontOfSize:20]];
[bt.titleLabel setTextAlignment:NSTextAlignmentCenter];
[bt.titleLabel setTextColor:[UIColor blueColor]];
[bt setBackgroundColor:[UIColor whiteColor]];
[bt addTarget:self action:#selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[mView addSubview:bt];
if (section<updateDataArray.count-1) {
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 2, 320, 0)];
lineView.backgroundColor=[UIColor lightGrayColor];
[bt addSubview:lineView];
[lineView release];
}
mView.backgroundColor=[UIColor clearColor];
[mView addSubview:title];
return mView;
}
Please suggest how to create a swipe to delete button functionality like table view row? I want that button in section header as well.
Add this lines in viewForHeaderInSection before returning your view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
....
// Your code here
....
mView.tag = section;
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(headerViewSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[mView addGestureRecognizer:sgr];
return mView;
}
- (void)headerViewSwiped:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gestureRecognizer.view;
// use mView.tag property to get the section index which you require to delete from array
// update your sectionDataSource array remove all the objects from section array
// so that it will reflect to numberOfSection method
// then remove all the rows which that section containing from your rowDataSource array
// so that it will reflect to numberOfRowsInSection method
// now call [tableView reloadData] method
}
}
This is a base idea to achieve what you required, change this code as per your project requirements.
Hope this helps.
Here are codes DMSLidingCell and TISwipeableTableView. And here is way how to make your own one. make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views. Now if you want to implement this to your header or any other view. Just change the tableview cell to your desired view.
you can add UISwipeGestureRecognizer to every HeaderView
Try with following way
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * viewTemp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[viewTemp setBackgroundColor:[UIColor redColor]];
[viewTemp setTag:(section + 100)];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen:)];
swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft );
[viewTemp addGestureRecognizer:swipeGesture];
return viewTemp;
}
- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
UIView * viewTemp = (UIView *) [self.tableView viewWithTag:[gesture.view tag]];
if(viewTemp){
NSLog(#"Do your stuff");
}
}
Add yourView and deleteButton to a container view, add swipe left gesture to the yourView.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, thisHeaderHeight)];
//your view
YourView* view;
view.frame = CGRectMake(0, 0, self.view.frame.size.width, thisHeaderHeight);
//....your code
//add button
UIButton* _customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_customButton setTitle:NSLocalizedString(#"Delete", nil) forState:UIControlStateNormal];
_customButton.frame = CGRectMake(self.view.frame.size.width - thisButtonWidth, 0, thisButtonWidth, thisHeaderHeight);
[_customButton addTarget:(id)self action:#selector(onPressDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
//add swipe gesture
UISwipeGestureRecognizer* slgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[slgr setDirection:UISwipeGestureRecognizerDirectionLeft]; // change direction accordingly
[view addGestureRecognizer:slgr];
UISwipeGestureRecognizer* srgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[srgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[view addGestureRecognizer:srgr];
//view covers customButton
[mainView addSubview:customButton];
[mainView addSubview:view];
return mainView;
}
That's swipe gesture function.
-(void)onSwipeHeader:(UISwipeGestureRecognizer *)gr{
if (gr.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gr.view;
CGRect newFrame = mView.frame;
if(gr.direction == UISwipeGestureRecognizerDirectionLeft){
newFrame.origin.x = -thisButtonWidth;
}else if(gr.direction == UISwipeGestureRecognizerDirectionRight){
newFrame.origin.x = 0;
}
[UIView animateWithDuration:0.25 animations:^{mView.frame = newFrame;}];
}
}
Finally, write code to performance delete.

How can I make button in section header for uitableview stay selected

I have a button that I want to change the background image if its selected. The code I have here works but if I scroll up and down again it becomes unselected.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
[button setBackgroundImage:[UIImage imageNamed:#"unselected_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
return sectionHeader;
}
- (void)buttonPressed:(id)sender {
UIButton *allButton = (UIButton*)sender;
allButton.selected = !allButton.selected;
}
1) Create an image name array in you ViewDidLoad. This array tracks the name of you photos whether if its been clicked or not.
- (void)viewDidLoad
{
//this array is just an example, loop and add items to the array for the number of sections you want
//in this example there are 5 sections with 5 images
imageNameArray = [[NSMutableArray alloc] initWithObjects:#"unselected_image", #"unselected_image", #"unselected_image", #"unselected_image", #"unselected_image", nil];
}
2) Your viewForHeaderInSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
[button setBackgroundImage:[UIImage imageNamed:[imageNameArray objectAtIndex:section]] forState:UIControlStateNormal];
[button setTag:section];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
return sectionHeader;
}
3) If button is pressed change the name of you image in the Array
- (void)buttonPressed:(id)sender
{
UIButton *allButton = (UIButton*)sender;
[imageNameArray insertObject:#"selected_image" atIndex:[allButton tag]];
[allButton setBackgroundImage:[UIImage imageNamed:[imageNameArray objectAtIndex:[allButton tag]]] forState:UIControlStateNormal];
}
What happens here is that every time the view becomes visible, you actually create a completely new view with everything reset. Instead of doing that you could simply save the view and re-use the same view every time. Create the view in viewDidLoad instead:
- (void)viewDidLoad
{
sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
[button setBackgroundImage:[UIImage imageNamed:#"unselected_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
}
viewForHeaderInSection simply becomes:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return sectionHeader;
}
Please note that if you have more than one section you would need to create an array of section header views and return the correct one for each section, based on the value of the section in viewForHeaderInSection
That happen because when table scroll up and down it will again create cell so you have to manage it in some way which button is selected or not and again set selected state to those button which are previously selected in viewForHeaderInSection you can do that as bellow
//Define in header file
NSMutableArray *mutArrSelected;
//Initlise in viewdidload
mutArrSelected = [[NSMutableArray alloc] init];
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(mutArrSelected.count < section)
{
[mutArrSelected addObject:[NSNumber numberWithBool:NO]];
}
UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
sectionHeaderLabel.text = [NSString stringWithFormat:#"Section %i",section+1];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
if([[mutArrSelected objectAtIndex:section] boolValue])
{
button.selected = YES;
}
button.tag = section;
[button setBackgroundImage:[UIImage imageNamed:#"unselected_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeader addSubview:button];
return sectionHeader;
}
- (void)buttonPressed:(id)sender
{
UIButton *allButton = (UIButton*)sender;
allButton.selected = !allButton.selected;
[mutArrSelected removeObjectAtIndex:allButton.tag];
[mutArrSelected insertObject:[NSNumber numberWithBool:allButton.selected] atIndex:allButton.tag];
}

How to set subview as a first responder?

I am trying to close the image View while clicking on close button.but button does not responds to touch event.
here is my code:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:#"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}
For the UIImageView to handle touches you need to set userInteractionEnabled to YES, it defaults to NO.
Try:
[imageView setUserInteractionEnabled:YES];
Reference
There is 1 property of UIImageView userInteractionEnabled and its value is by default NO. So in your code add
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;
Maybe there's another view with tag=1 in your self.view. Please try to edit button clicked like this:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}

Not able to identify the subview of TableView in Iphone

I added a header view with a button & a imageview but I can not Identify the Imageview for changed the Image.
My code is as follow:
My HaderView class .h file (HeaderSection.h)
#import <UIKit/UIKit.h>
#interface HeaderViewSection : UIView
#property(nonatomic,retain)UIButton *btn;
#property(nonatomic,retain)UIImageView *arrow_img;
#end
My HaderView class .m file (HeaderSection.m)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor brownColor];
_arrow_img=[[UIImageView alloc]init];
_arrow_img.frame=CGRectMake(280, 15, 20, 20);
_arrow_img.image=[UIImage imageNamed:#"Arrow_down_section.png"];
_arrow_img.backgroundColor=[UIColor clearColor];
[self addSubview:_arrow_img];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 15, 35, 25);
[btn setTitle:#"R" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor blackColor];
[self addSubview:btn];
}
return self;
}
.h file
#import "HeaderViewSection.h"
#property (nonatomic,retain)HeaderViewSection *header_view;
.m file
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
_header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_header_view.tag = section+5000;
_header_view.btn.tag = section+5000;
[_header_view.btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
_header_view.arrow_img.tag=section+2000;
return _header_view;
}
-(void)expandTableHeaderView:(id)sender{
[self.tblView beginUpdates];
NSInteger section = [sender tag];
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]]; //RETURNS nil
NSArray *arr = [view subviews]; //RETURNS nil
}
I don't know why this happen? Please Help me to solve this.
In this method you are creating new headerView. Now at this time there is no subView in headerView.
You are setting headerView.btn.tag but you did not add button in headerView, same is the case with image.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
_header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_header_view.tag = section+5000;
UIButton *button = [UIButton alloc] init];//set frame also
button.tag = section+5000;
[button addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
[header_view addSubView:button];
//Do same like image view
}
Edied:
As -(void)expandTableHeaderView:(id)sender{ } method will call only when you tap on button. Button is a subView of headerView. You can have header view by calling its superView method like this
UIView *view = (UIView*)[sender superView];
view.frame = // new frame
now view is you headerView, you can change its frame to expand or what ever you want.
You can get its subViews like this
NSArray *array = [view subViews];
for(UIView *v in array){
if([v isKindOfClass:[UIImage class]]){
//v is imageview change image if you want
}

Resources