I created a button inside of UITableViewCell, and when I click a button ,I want go to author view control with index value.
tapped = [UIButton buttonWithType:UIButtonTypeCustom];
[tapped setFrame:CGRectMake(0, 0, 320, 100)];
[tapped setTitle:#"button" forState:UIControlStateNormal];
NSInteger tet;
tet = indexPath.row;
[tapped addTarget:self action:#selector(tapped:) forControlEvents: UIControlEventTouchDown];
[Cell addSubview:tapped];
You should tag a button with indexPath.row.
tapped.tag = indexPath.row;
Inside your event handling code, you should use that tag to find index.
-(void) tapped:(UIButton *)sender
{
UIButton *btn = (UIButton *)sender;
int index = btn.tag;
//Do rest...
}
you can tag the button as follows :
tapped.tag = indexPath.row
And in the the tapped method You can use it as follows:
-(IBAction)tapped:(id)sender
{
UIButton *btn = (UIButton *)sender;
int index = btn.tag;
}
use this index as per your requirement...
try this
-(void) tapped:(UIButton *)sender
{
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [myTableView indexPathForCell:clickedCell];
int section = indexPath.section;
// You get easily your index path row
int row = indexPath.row;
// push your controller
}
first update ur code
use ur event UIControlEventTouchUpInside
tapped = [UIButton buttonWithType:UIButtonTypeCustom];
[tapped setFrame:CGRectMake(0, 0, 320, 100)];
[tapped setTitle:#"button" forState:UIControlStateNormal];
NSInteger tet;
tet = indexPath.row;
[tapped addTarget:self action:#selector(tapped:) forControlEvents: UIControlEventTouchUpInside];
[Cell addSubview:tapped];
Related
I have a UITableViewCell in which I'm creating some buttons (which are acting like radiobuttons) and assigning tags to the buttons. But the tags are not static. They are dynamic. Now when a particular radiobutton is clicked I need to set that button with some image (radio_On.png) and remaining all buttons as (radio_Off.png). But to set the images I couldn't understand how to get the tag values of all buttons in that particular cell because they are not static.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height+3,17, 17)];
[btTemp1 addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp1 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp1.tag=++Optionid;
[hlcell.contentView addSubview:btTemp1];
btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,17, 17)];
[btTemp2 addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp2 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp2.tag=++Optionid;
[hlcell.contentView addSubview:btTemp2];
}
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
[sender setImage:[UIImage imageNamed:#"radio-On.png"] forState:UIControlStateNormal];
}
Here one radiobuttton is not getting released when I check other button in TableView cell.
EDIT:
Prevoiusly I gave this code:
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
UIButton *btnTemp1 = (UIButton*)[cell viewWithTag:10]; //Static .But now I want dynamic
UIButton *btnTemp2 = (UIButton*)[cell viewWithTag:11];//Static .But now I want dynamic
UIButton *btnTemp3 = (UIButton*)[cell viewWithTag:12];//Static .But now I want dynamic
UIButton *btnTemp4=(UIButton *)[cell viewWithTag:13];//Static .But now I want dynamic
UIButton *btnTemp5=(UIButton *)[cell viewWithTag:14];//Static .But now I want dynamic
[radioButtonsinaSection addObject:btnTemp1];
[radioButtonsinaSection addObject:btnTemp2];
[radioButtonsinaSection addObject:btnTemp3];
[radioButtonsinaSection addObject:btnTemp4];
[radioButtonsinaSection addObject:btnTemp5];
}
for(int i=0;i<[radioButtonsinaSection count];i++){
[[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-On.png"] forState:UIControlStateNormal];
}
How can I get the all the controls (buttons and their tags) of a cell in which button (radiobutton) is clicked ?
As per my knowledge you miss the loop in radioButtonClicked method,which keeps track of which button is clicked..you try this code..
(IBAction) radioButtonClicked:(UIButton *) sender{
for(int i=0;i<[self.radioButtons count];i++){
[[self.radioButtons objectAtIndex:i] setImage:[UIImage
imageNamed:#"radio-off.png"]
forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-On.png"]
forState:UIControlStateNormal];
}
I think it helps you,if not feel free to ask again..
Enjoy..
I'm using three tables in my view and seprating them each other using a tag. In my second table view i made a cutom uitableview and adding labels and buttons inside them .
this is the button which i wrote inside the cellforrowatindexpath
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:#selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:#"following12.png"] forState:UIControlStateNormal];
following.tag=indexpath.row;
[followingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
======
-(void)followingButtonpressed:(id)sender
{
UIView *contentView1 = (UIView *)[sender superview];
UITableViewCell *cell1= (UITableViewCell *)[contentView1 superview];
NSIndexPath *indexPath1 = [followingTable indexPathForCell:cell1];
[sender tag];
NSLog(#"sender tag --%d",[sender tag]);
// UIButton *button = (UIButton *)sender;
// UITableViewCell *cell = (UITableViewCell*)[button superview];
UIButton *tempButtom = (UIButton *)[cell1 viewWithTag:[sender tag]];
[tempButtom setImage:[UIImage imageNamed:#"following_off12.png"]
forState:UIControlStateNormal];
}
But its crashing and i was not able to change the image of selected button , please help
Use this to detect which button has been pressed
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
Give Tag to each UIButton and write following code for detect which button is pressed
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
// Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
(UITableViewCell*)cell = [[button superview] superview];
int row = [myTable indexPathForCell:cell].row;
// Here **row** give you tad of specific UIButton that you tapped
if (row == yourTag )
{
// write code for action
}
}
Or you can set tag for cell each time, but not UIButton. Your action will look then:
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
// Here **row** give you tad of specific UIButton that you tapped
if (row == yourTag )
{
// write code for action
}
}
I need to add a UIButton in my UITableView only with my first and last array count. I found that we can use tableFooterView, to add unbutton below our tableview. But how can I achieve this over my tableview and only with first and last array values? Here is my code,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Adding a UIButton in last row
NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
NSLog(#"lastSectionIndex:%d",lastSectionIndex);
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
NSLog(#"lastRowIndex:%d",lastRowIndex);
// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
NSLog(#"last index:%#",pathToLastRow);
if (pathToLastRow.row == lastRowIndex)
{
NSLog(#"row enters");
checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
[checkButton1 addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[checkButton1 setBackgroundImage:[[UIImage imageNamed:#"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
editTable.tableFooterView = checkButton1;
[cell addSubview:checkButton1];
}
Now I receive the buttons in every cell of my tableview. How can I give the button only to my first and last row array values? Thanks in advance.
Change your if condition as,
if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
{
It will look like this,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
checkButton1.tag = 100;//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
[checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
[checkButton1 addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[checkButton1 setBackgroundImage:[[UIImage imageNamed:#"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell addSubview:checkButton1];
}
//Adding a UIButton in last row
NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
NSLog(#"lastSectionIndex:%d",lastSectionIndex);
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
NSLog(#"lastRowIndex:%d",lastRowIndex);
// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
NSLog(#"last index:%#",pathToLastRow);
UIButton *checkButton1 = (UIButton *)[cell viewWithTag:100];//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
{
checkButton1.hidden = NO;
} else {
checkButton1.hidden = YES;
}
You dont have to declare checkButton1 in .h file. Make it a local variable as shown above. Then you can set hidden property to hide/show in difference cells. Instead of doing the above, you can also create this button in custom UITableViewCell class and set the hidden property as cell.checkButton1.hidden = YES. You need to subclass UITableViewCell for that.
instead of creating Button in CellforRow You can Create in ViewDidload And attach it with FooterView of table.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[aButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
[aButton setTitle:#"Click me" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
[aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
[self.tableView setTableFooterView:aButton];
Same thing you can do for headerview. or else You Can Use Viewforheader method
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; // x,y,width,height
UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
reportButton.frame = CGRectMake(80.0, 0, 160.0, 40.0); // x,y,width,height
[reportButton setTitle:#"rep" forState:UIControlStateNormal];
[reportButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:reportButton];
return headerView;
}
You should add first button to your table view Header And other button should be added to your footer view Secion of tableView
I am adding Radio Buttons in custom cell of UITableView and calling a method on it to changes its image or background Image. I able to set Selected button Image from sender but I want to make other buttons to their original position which I am unable to set it, as below functionality need to be look like proper Radio buttion in a cell
I am also facing problem in fetching proper Index Path in this method - (void) TableRadioButtonSelection:(id)sender;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *RadioIdentifier = #"RadioCellIdentifier";
RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier];
if(myRadiocell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"RadioBtnCell" owner:self options:nil];
myRadiocell = radioCell;
//[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn1.tag = ButtonTag;
//[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn2.tag = ButtonTag1;
//[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn3.tag = ButtonTag2;
myRadiocell.tag = RadioTag;
[myRadiocell.radioBtn1 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn2 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn3 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
myRadiocell.backgroundView.backgroundColor = [UIColor clearColor];
self.radioCell = nil;
}
return myRadiocell;
}
- (void) TableRadioButtonSelection:(id)sender {
//RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview];
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag];
UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1];
UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2];
[mybtn setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[mybtn1 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[mybtn2 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
//NSLog(#"%#",[cell subviews]);
UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added.
UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1];
UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2];
// I am unable to set Image of other buttons apart from sender.
//for(UIView *item in [mysubview subviews]) {
if ([mysubview isKindOfClass:[UIButton class]]) {
UIButton *newBtn = (UIButton*)mysubview;
UIButton *newBtn1 = (UIButton*)mysubview1;
UIButton *newBtn2 = (UIButton*)mysubview2;
NSLog(#"OK %#",newBtn);
//[newBtn setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn1 setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn2 setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
}
//}
NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell];
NSLog(#"%d",indexPath.row);
// Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image
UIButton *btn = (UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"radiobtn_chk.png"] forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:#"radiobtn_chk.png"] forState:UIControlStateNormal];
}
try changing this line to this;-
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
to
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*];
In this way you can access the table view cell.After that you can get the button from cell content views and do further coding.
Suppose if you do not know the indexPath then you can create the indexPath fromm the rowNumber of the table view(I think you have stored the table row number in RadioTag, if yes so you can use this field as a parameter).
I have made a table and contents some values, I have a button coding like
cell.textLabel.textColor = [UIColor whiteColor];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:#"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:#selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
I want to click that button and take values from that particular index,
like
Ali (button here) --------index 0
Sajid (button here) --------index 1
Robert (button here) --------index 2
RaM (button here) --------index 3
Theser are cells of table, now how can I get index as I click that button?
if you are not getting my question, you can ask me again...
I found my answer,
Just write the following coding inside the button targeted method and use clickButtonPath as your index path.
It is working perfectly, simply
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
btn.tag = indexPath.row;
Then in (void)editTable:(id)sender:
int row = (UIButton *)btn.tag;
you would use something like this.
- (IBAction)editTable:(UIButton *)sender {
UIView *contentView = [sender superView];
UITableViewCell *cell = [contentView superView];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
But first you should make sure to properly reuse your cells. By changing your code like this:
cell = ... dequeue cell ...
if (cell == nil) {
cell = ... create new cell ...
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = 194;
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:#"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:#selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
}
// if you need to change something for this button you can get it like this:
//UIButton *btn= [cell.contentView viewWithTag:194];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;