UITableView section clicked in Viewforheaderinsection - iphone

I have a created custom UIButton in UITableView viewForHeaderInSection. Now when i click on the button within the section how will i know which section is clicked? Please help.

when you add button in viewForHeaderInSection , just set tag with section number like bellow.,..
button.tag = section;
and when your button clicked at that time you can get that number with bellow code...
- (IBAction)yourButton_Clicked:(id) sender
{
NSLog(#"Section Number => %d",[sender tag]);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *btn =[[UIButton alloc]init];
btn.tag=section;// this tag property will differentiate button for different section
}
by this you can access btn and add event on that burron

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *button =[[UIButton alloc]init];
button.tag=section;// this tag property will differentiate button for different section
}

In didselectrowatindexpath check for if(index path.section) is equal to the section you want the action.

Related

Get row number of UITableView in IOS [duplicate]

This question already has answers here:
get section number and row number on custom cells button click?
(5 answers)
Closed 9 years ago.
In my code I have a table view with many sections sorted alphabetically. I have checkboxes(UIButton) in table rows and I need check them according to respective status entered in row. I had done
checkbutton.tag = indexpath.row;
but when table scrolls and section get changed and I get status of previous section in my checkbox method. anyone please help me on this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
[CheckButton addTarget:self action:#selector(CheckBoxAction:) forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
//some other stuff
[cell.contentView addSubview:CheckButton];
}
and this my CheckBoxAction
-(void)CheckBoxAction:(UIButton *)btn
{
tag = btn.tag;
NSDictionary *tagdict =[statusArray objectAtIndex:tag];
}
Now, problem is that indexpath.row is 0 whenever section is changed, so I can't able to access exact row number in my CheckBoxAction.
use this for getting current row you tap.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckButton = [[UIButton alloc] init];
[CheckButton addTarget:self action:#selector(CheckBoxAction:) forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
[cell addSubview:CheckButton];
}
-(void)CheckBoxAction:(id)sender
{
// Cast Sender to UIButton
UIButton *button = (UIButton *)sender;
// Find Point in Superview
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
// Infer Index Path
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];
// Log to Console
NSLog(#"selected Row : %d", indexPath.row);
}
// Try this
CheckButton.tag = [[NSString stringWithFormat:#"%d%d",indexPath.section,indexPath.row] integerValue];
or
// try this below link to get logic for, how to handle checkbox in UITableView
Set checkmark in UITableView
What you are doing is wrong in many ways. So, I am not going to give you a solution for the exact problem you have, but more of a guideline:
First you are adding the checkbox directly to the cell, which means you are possible adding multiple CheckButton to the same cell, if you scroll your UITableView a few times.
Add the checkButton on the cell directly instead of keep adding it in the cellForRowAtIndexPath, this can give you a performance boost.
Use a block that will be executed when a user clicks the button. This way you are able to capture the context of your UIViewController. You should assign that block to the cell on cellForRowAtIndexPath

Change a button dynamically in a table view cell

I have a table where 5 cells are there. In each cell there is a button. I need to change those buttons dynamically on some conditions. How to change the button dynamically in table view.
What you can do is Write a function which will change the values of the buttons and then you can call that function. After changing button values then use any one of the mentioned method below:
You can use either : [tableview reloadData]; to reload all the table data.
OR
You can reload particular rows using the following method :
[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];
You can query your table view for all visible cells through -visibleCells, get the reference of your button (assuming you have a UITableViewCell subclass with a property for the button) and change them.
First you need to set tags for each buttons. This can be done by
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your code
cell.Button.tag = indexPath.row;
[cell.Button addTarget:self action:#selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];
}
Then you can get the button from this method
- (IBAction)buttonClick1:(id)sender {
int tagNo =[sender tag];
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]];
//Now change the action for the cell
[cellclicked.Button addTarget:self action:#selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];
//Your Code
}
You can follow the same steps for - (IBAction)buttonClick2:(id)sender
Now you can track which button was clicked and change the method for that button.
And declare your cell and button strong.

Not getting action when pressing UIButton inside a custom static cell

Using iOS5. I dragged a UIButton object to inside a custom static cell. Since I am not using dynamic properties I am not using the data source template. The thing is that I am unable to trigger any event when pressing that UIButton inside the cell. I am running the folioing code in my table view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
NSIndexPath *myIP = [NSIndexPath indexPathForRow:1 inSection:1];
UITableViewCell *cell = [medicareTableView cellForRowAtIndexPath:myIP];
UIButton *stepNumberOfChildren = (UIButton *)[cell viewWithTag:222];
[stepNumberOfChildren addTarget:self action:#selector(dontTouchMe:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)dontTouchMe:(id)sender {
NSLog(#"Touched Button %i",((UIButton *)sender).tag);
}
Run the app, press the UIButton and it does not log anything... What am I doing wrong?
Connect your outlet to dontTouchMe function (TouchUpInside)
I hope you want to add the target only to the cell at row=1 and section=1
So code as follows,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row==1&&indexpath.section==1)
{
//add the button target
}
}

UITableViewCell with a UIButton

I have UITable which contains some UIButtons. I would like a way to use the label.
The problem I have is that I need two tag labels, one for retrieving the cell in tableviewCellWithReuseIdentifier and configureCell.
Until recently I have been using the UIButton.title to determine which row in the table I selected. The (invisible) text was the row number.
Now I need to use the title for some visible text. How can I still find which row was pressed?
When I press the area outside the button the usual didSelectRowAtIndexPath is called. The hard part is that I want to capture both the UIButton (which calls its own selector) and the row, which I had been finding out from the title. Now I need the title for something else.
I use something like this. This will get you the indexpath in the tableview.
- (IBAction)buttonAction:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint buttonOriginInTableView = [button convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonOriginInTableView];
// do something
}
You could subclass UITableViewCell or UIButton and add some properties.
You should implement the UITableViewDelegate protocol.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do something based on selected row (indexPath.row)
}
Implement the UITableViewDelegate protocol. It will help you and do what you need.
didSelectRowAtIndexPath is autmatically called whenever a row is selected in your table.
in your .m file the one with the table view controller if there isn't one there add this method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// NSInteger row = [indexPath row];
// or (indexPath.row)
// do whatever you like with this value which tells you which row is selected.
}
PK

how to add a button without using custom cell on a UITableView?

How do I add a custom button on a UITableViewCell, and then delete the cell with that button without using Interface Builder and Custom Cell?
If you really want to add a custom button WITHOUT subclassing, just add the button to the contentView of the cell:
[cell.contentView addSubview:customButton];
You can set all the characteristics of the button: frame, target, selector, etc... Ad then used the above call to add it to the cell.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=//whatever
[customButton setImage:anImage forState:UIControlStateNormal];
[customButton setImage:anotherImage forState:UIControlStateHighlighted];
[customButton addTarget:self action:#selector(delete) forControlEvents: UIControlEventTouchUpInside];
//yadda, yadda, .....
You can tag it as well
customButton.tag = 99999;
So you can find it later:
UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999];
You will need to decide WHEN to add the button, maybe on cell selection, maybe in editing mode... just put the code in the delegate method of your choice.
If the button's sole purpose is to offer deletion you should look into UITableViewDataSource which has a method called - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath. Implement it like so:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
And then implement:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Database removal code goes here...
}
To use these methods, let your UITableViewController implement the UITableViewDataSource protocol by doing something like:
MyClass : UITableViewController <UITableViewDataSource>
in your header file, and be sure to remember to set the viewController's datasource to self.