help on button.tag = indexPath.row - iphone

I have this code on my cellForRowAtindexPath, with a custom cell and a button on the each cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"tblCellView";
TableCellView *cell = (TableCellView *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
Alarms *alarma = (Alarms *)[alarmsArray objectAtIndex: indexPath.row];
if (!cell) {
[[NSBundle mainBundle] loadNibNamed:#"TableCellView" owner:self options:nil];
cell = tblCell;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-7, 4, 30, 33)];
[button addTarget:self action:#selector(favButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1];
[cell.contentView addSubview:button];
[button release];
}
// Set up the cell.
[cell setLabelText: [alarma nombreAlarma]];
UIButton *button = (UIButton *)[cell viewWithTag:1];
button.tag = indexPath.row;
return cell;
}
My problem is that when I click on the button, I got random results, as soon as I move on the table and cells are reusing I got different indexes that are not equal to label tex for the cell in question.
-(IBAction)favButtonAction: (id)sender
{
UIButton *button = (UIButton *)sender;
Alarms *alarma = (Alarms *)[alarmsArray objectAtIndex: button.tag];
NSLog(#"labelText: %#",[alarma nombreAlarma]);
}
for example, the first cell always is ok but the last cell is equal to first objectAtIndex (maybe button.tag is equal to 0 when it must be 14?)

Ok I found a solution, is a different approach but it works :
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
and then on the button handler:
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
...
}
}
more info here Detecting which UIButton was pressed in a UITableView

Don't put tag value for button inside cell.
This is my button inside the content view of my cell. Note: button should be subview of Contentview.
[cell1.YourButton addTarget:self action:#selector(OnClickCategory:) forControlEvents:UIControlEventTouchUpInside];
Action for my button
- (void) OnClickCategory:(id) sender {
//NSIndexPath *indexPath =(NSIndexPath *)[sender tag];
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [CategoryTable indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
NSLog(#"%d",indexPath.row);
}
This works for me.

Related

Selecting UITableViewCell AccessoryView, separate from selecting row

I have a UITableview that I'm displaying some tasks and each row has a checkbox to mark tasks complete or not.
I'm looking to toggle the checkmark when the user taps on the checkbox, and switch to a detailed view when the user taps on the row. The latter is easy, just by using
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
However, I wanted to separate the selection areas, toggling only the checkbox when the accessoryview is selected, and going into detailed view only when the rest of the cell is selected. If I add a UIbutton inside the accessoryview, the user would be selecting the row AND the UIButton when they only wanted to hit the checkbox, right?
Also, what if the user was just scrolling through the tableview by dragging along the accessoryview? Wouldn't this trigger an action on the UIButton on TouchUp?
Anyone have any ideas on how to do this? Thanks for your time!
How about managing accessory taps inside this delegate method:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
EDIT:
You can do something like this for custom accessoryView that responds to accessoryButtonTappedForRowWithIndexPath: method.
In cellForRowAtIndexPath: Method -
BOOL checked = [[item objectForKey:#"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"checked.png"] : [UIImage imageNamed:#"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:#"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:#"checked"];
UITableViewCell *cell = [item objectForKey:#"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:#"unchecked.png"] : [UIImage imageNamed:#"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}

pass two arguments on button clicked (access two strings on button click)

I am a new Programmer of i phone
i have a little problem.... i wants pass two arguments on button click....
//---insert individual row into the table view---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"Cell %i",indexPath.section]];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:#"Cell %i",indexPath.section]] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;//my code.... change the color or selected cell
search_items *pro = [searchCount objectAtIndex:[indexPath row]];
NSString *string1=[[NSString alloc]init];
NSString *string2=[[NSString alloc]init];
/// i wants access these two strings on buttonClicked Method...(pass as a argument):)
string1=pro.s_showroomName;
string2=pro.s_productName;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(150, 15,150,40);
[aButton setTag:indexPath.row];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aButton];
UIImage *img = [UIImage imageNamed:#"image.png"];
[aButton setImage:img forState:UIControlStateNormal];
[aButton setTitle:#"View Details" forState:UIControlStateNormal];
[aButton addSubview:buttonLabel1];
NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:#"png"];
UIImage *image=[UIImage imageWithContentsOfFile:filepath];
cell.imageView.image=image;
return cell;
}
-(void)buttonClicked:(UIButton*)sender {
//int tag = sender.tag;
}
One simple method to access your cell from the button, in the button callback is to ask it for it's superview's superview:
-(void)buttonClicked:(UIButton*)sender {
//int tag = sender.tag;
UIView* contentView = [sender superview];
if ([[contentView superview] isKindOfClass:[UITableViewCell class]] == NO) {
NSLog(#"Unexpected view hierarchy");
return;
}
UITableViewCell* cell = (UITableViewCell*)[contentView superview];
}
You can also, based on the frame of the button, find out which cell it belongs to in the table view. You would need to convert the frame from the button to the table view (UIView has methods to do the conversion).
The default UITableViewCell objects have a textLabel and a detailTextLabel, in your cellForRowAtIndexPath method you can set their text like this:
cell.textLabel.text = #"Some text";
cell.detailTextLabel.text = #"Some detail text";
then you can access these properties in your callback, when you have obtained the cell.

Passing argument in button action method without use of tag

i am creating button dynamically. and creation action methods by
[newButton addTarget:self action:#selector(goToNew:)forControlEvents:UIControlEventTouchUpInside];
i want to send argument (indexpath.row) from tableview , but not want to use tag.. because i need that tag will remain same for all the buttons , how can i pass argument in button action ?
actually i am adding button in each tableview cell , and i want action for all those buttons , but if i use tag = indexpath.row and and use it with action, it works but problem of overlaying button happen.hence i want tag would be constant.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UIButton *btn;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(goToNew:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 55;
[cell.contentView addSubview:btn];
}
else {
btn = (id)[cell.contentView viewWithTag:55];
}
return cell;
}
- (void) goToNew:(id)sender
{
UIButton *b = (UIButton *)sender;
UITableViewCell cell = (UITableViewCell)[b superview];
int row = [msgTableView indexPathForCell:cell].row;
(#"row is :::%d",row);
}
By doing following code, you will get the indexPath.row and no need to set the button tag.
- (IBAction)goToNew:(id)sender
{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) [btn superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
int row = indexPath.row;
}
You can pass another argument as set title of button using different forstate.Here you can pass indexpath as title of button.like
btn.tag=10;
[btn setTitle:[NSString stringWithFormat:#"%d",indexpath.row] forState:UIControlStateDisabled];
[btn addTarget:self action:#selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
and you will received argument as
-(void)btnClick:(id)sender
{
UIButton* b = (UIButton*) sender;
NSLog(#"tag=%d",b.tag);
//Below line will got indexpath in string formate..
NSLog(#"title =%#",[b titleForState:UIControlStateDisabled]);
}
-(void)goToNew:(id)sender
{
UIButton *btnTemp = (UIButton *)[yourTableView viewWithTag:sender];
int iTag = btnTemp.tag;
}

can icon on UITableViewAccessory replace?

i have a table view, in one condition i want to use an accessory using UITableViewCellAccessoryDetailDisclosureButton, but i want to change that icon with my icon, can i do it??
yes ....you can do it easily by adding custom button...
Create the Custom button,
Setting the image of button
Add this button to accessoryView
Set the method for the button
I am providing you some help with the Code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell =(Custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[Custom alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell setBackgroundColor:[UIColor clearColor]];
menuTable.separatorColor =[UIColor clearColor];
}
UIImage *image = [UIImage imageNamed:#"ButtonClickOrder.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
//[button setBackgroundImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
//These are the methods which are called with the AccessoryView is Clicked
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:menuTable];
NSIndexPath *indexPath = [menuTable indexPathForRowAtPoint: currentTouchPosition];
NSLog(#"Section:%d",indexPath.section);
NSLog(#"Index:%d",indexPath.row);
if (indexPath != nil)
{
[ self tableView: menuTable accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%d",indexPath.section);
}
Hope this will surely work for u....

identifying Button selection of each row in Tableview?

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
[btn setTitle:#"OK" forState:UIControlStateNormal];
[btn setTitle:#"OK" forState:UIControlStateSelected];
[btn addTarget:self action:#selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
return cell;
}
but if the user selects the button on particular row, i can change that image through onClickRename...but can i get inwhich row's image has been touched through
(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath)indexPath ?
you could do something like
[btn setTag:[indexPath] row]
in your cell setup, and then
- (void) onClickRename:(id)sender {
int row = sender.tag;
}
you'd know which table row was hit.
Apple uses the following technique in their Accessory sample code:
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
// do what you want with the item at indexPath
}
}
In Swift, you can set a tag for each button
Also, set each index value as its tag, we get to know which button is clicked at which row.
to set tag, in cellForAtIndexPath
cell.button.tag = indexPath.row
in button Action
#IBAction func anAction(_sender : AnyObject){
let tappedButton = sender.tag // gives the row and button
}