Not able to identify the subview of TableView in Iphone - ios

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
}

Related

Button creates at tableFooterView not response to events

I create a tableView in a class inherit from UIView.It works well when I add a button at tableFooterView on this tableView.
CustomView.h
#import <UIKit/UIKit.h>
#interface CustomView : UIView<UITableViewDelegate,UITableViewDataSource>
#end
CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
UIButton * tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
tempButton.center = customView.center;
tempButton.backgroundColor = [UIColor redColor];
[tempButton addTarget:self action:#selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:tempButton];
return customView;
}
- (void)doButtonAction{
NSLog(#"123");
}
NSLog:123
However,I public this button and make a target-action in my controller.It response nothing.
CustomView.h
#property (strong, nonatomic) UIButton * tempButton;
CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
self.tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
self.tempButton.center = customView.center;
[customView addSubview:self.tempButton];
return customView;
}
ViewController.h
- (void)viewDidLoad {
[super viewDidLoad];
CustomView *cv = [[CustomView alloc] initWithFrame:self.view.bounds ];
[cv.tempButton addTarget:self action:#selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cv];
}
- (void)doButtonAction{
NSLog(#"123");
}
what did I miss?
In the viewdidload you are not set any thing on the button,
You just assign in the #interface.
but you must be alloc and set the frame and backgroundcolor and all properties on the button in the viewdidload .
After that it will work.
May be it will help you or feel free.

customize UITableView Header Section at run time

I have set header back ground using the code below but I want to change the color of header back again on run time when I click on the button that is added as a subview on the header.
please provide me code, thanks in advance :)
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.bounds.size.width, 50)];
header.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homeCellHeaderBackGround.png"]];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
label.text = _array[section][#"name"];
label.textColor = [UIColor whiteColor];
[header addSubview:label];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(230, 10, 70, 35)];
[button addTarget:self action:#selector(onExpandButton:) forControlEvents:UIControlEventTouchUpInside];
//[button setTitle:#"Expand" forState:UIControlStateNormal];
[button setTag:section];
[header addSubview:button];
return header;
}
You can modify it in run time by:
first:
you can declare a global variable/propery like:
#property (nonatomic) UIView *tableHeader;
and set it under -(void)viewDidLoad
like
self.tableView.tableHeaderView = self.tableHeader;
or using the delagate:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.tableHeader;
}
and modify it anywhere you like, but dont forget to reload you table section header, probably [tableView reloadSections:NSIndexSet withRowAnimation:UITableViewRowAnimation]; will do.
But
In your case you can retreive/get the tableHeader made inside the delegate:
-(UIView *)tableView:tableView viewForHeaderInSection:section
by
UIView *tableHeaderView = [self tableView:yourTableView viewForHeaderInSection:yourSection];
then modify tableHeaderView..
or simply reassign tableHeader by:
yourTableView.tableHeaderView = tableHeaderView;
your by using header alone, since as i can see it's a global variable..
change/update it directly like:
header.backgroundColor = [UIColor yourNewColor];
hope i've helped you.. happy coding, cheers..
You can change the background color of the section header by following code:
- (IBAction)onExpandButton:(id)sender {
UIView *header = ((UIButton *)sender).superView;
header.backgroundColor = <Whatever UIColor you like>;
...
}
-(IBAction)onExpandButton:(UIButton *)sender
{
UIView *tmpView = [self.YourTableViewName headerViewForSection:sender.tag];
tmpView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homeCellHeaderBackGround.png"]];
}
-You got the view from your table view header for section method. After you can set your image view on view.

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.

Can't Able to access the Header View in Iphone

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.

how to stop custom UITableViewCell covering UITableView border lines

Perhaps this is a very simple answer, but I have searched everywhere and it seems everyone is trying to do the opposite of what I am trying to do. I have a custom UITableViewCell as defined below.
#import "TagCell.h"
#import "TagRepository.h"
#import "TagsViewController.h"
#implementation TagCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self setFrame:CGRectMake(0, 1, self.frame.size.width, 37)];
self.selectionStyle= UITableViewCellSelectionStyleNone;
self.textLabel.hidden=NO;
[self.textLabel setFrame:CGRectMake(self.bounds.size.width/2-40, self.bounds.origin.y+1, 300, 30)];
self.textLabel.text= #"test";
[self.textLabel setFont:[UIFont fontWithName:#"Helvetics" size:30] ];
}
return self;
}
-(void) layoutSubviews
{
NSArray *viewsToRemove = [self subviews];
for (UIView *v in viewsToRemove) {
if([v isKindOfClass:[UIButton class]])
{
[v removeFromSuperview]; //used to remove and redraw the button during a orientation change
}
}
UIButton * removeButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[removeButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:18] ];
[removeButton setFrame:CGRectMake(7*self.bounds.size.width/8, self.bounds.origin.y+3, 30, 30)];
removeButton.hidden=NO;
[removeButton addTarget:self action:#selector(removeTag) forControlEvents:UIControlEventTouchUpInside];
[removeButton addTarget:self action:#selector(removeTag) forControlEvents:UIControlEventTouchDragInside];
[removeButton setBackgroundImage:[UIImage imageNamed:#"button_red_unselect.png"] forState:UIControlStateNormal];
[removeButton setBackgroundImage:[UIImage imageNamed:#"button_red_select.png"] forState:UIControlStateSelected];
[removeButton setImage:[UIImage imageNamed:#"icon_minus.png"] forState:UIControlStateNormal];
[removeButton setImage:[UIImage imageNamed:#"icon_minus.png"] forState:UIControlStateSelected];
[self addSubview:removeButton];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) removeTag
{
for( int i=0 ; i< [TagRepository instance].tags.count; i++)
{
if([self.textLabel.text isEqualToString:[TagRepository instance].tags[i]])
{
[[TagRepository instance].tags removeObjectAtIndex:i] ;
}
}
_reloadTable();
// [_delegate.table reloadData];
//RELOAD TABLE
}
#end
So what happens is, that when I open up the tableview when there is an active custom cell the border is not present and it appears as if there is a blank cell following it.. or something.
This is not desired I wish the cell had a border surrounding it.
The view that contains the table sets heightforcellatindex:indexpath = 40, so that delegate function returns 40 as the cell height value, I assume that the problem lies between some sort of mismatch between this and the subviews in the custom cell. Do you see anything wrong with this??
note that I have already changed the typo in Helvetica.
If you add [super layoutSubviews] to your layoutSubviews method, the cell separators will appear. I'm not sure what you mean by the blank cell following it -- I didn't see anything like that when I tried your custom cell. Also, you should be adding (and removing) your buttons to the cell's contentView, not to the cell itself.

Resources