Accessorybuttontappedforrowwithindexpath: Not Getting Called

accessoryButtonTappedForRowWithIndexPath: not getting called

Did you just click on the cell to select it or did you actually click on the accessory button indicator on the cell? It isn't clear from your question.

accessoryButtonTappedForRowWithIndexPath is applicable when you click on the button icon within the cell and not when you select the cell.

accessoryButtonTappedForRowWithIndexPath not working for custom button or label

For custom button you can add target for specific event, accessoryButtonTappedForRowWithIndexPath will not work for custom view

Please refer:

Sample Image

Implementing accessoryButtonTappedForRowWithIndexPath: in Swift 2

Not sure why you are adding disclosure button indicator like that in code.

What you are looking for is simply 2 step process -

Step 1 : Add correct accessoryType on cell:

cell.accessoryType = .DetailDisclosureButton

Step 2 : Take action when the button is tapped by creating the accessoryButtonTappedForRowWithIndexPath function:

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
doSomethingWithItem(indexPath.row)
}

Custom accessoryView image and accessoryButtonTappedForRowWithIndexPath

Yes it should crash, because delegate of tableview should not be called from custom cell class, better you can use custom delegate. I am posting a sample code, similar to your case, when button tapped it call's the custom delegate method from that u can do whatever you want


   //in custom calss
#import <UIKit/UIKit.h>

//create a delegate
@protocol CustomDelegate<NSObject>
-(void)whenButtonTapped:(id)sender; //your delegate method
@end

@interface CustomCell : UITableViewCell<CustomDelegate>
@property(nonatomic, assign)id<CustomDelegate> delegate; //create delegate
@end

//.m file of custom cell
#import "CustomCell.h"

@implementation CustomCell
@synthesize delegate; //sysnthesize delegate

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //its your button
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectZero]; //for example i took label

[aButton addTarget:self action:@selector(whenButtonTapped:) forControlEvents:UIControlEventTouchUpInside];//adding target for button action
[aButton setTag:100];
[aLabel setTag:200];

[self addSubview:aButton];
[self addSubview:aLabel];

//since i am using without ARC
[aLabel release];
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}

-(void)dealloc
{
delegate = nil;
[super dealloc];
}

//i am setting the frames hear
-(void)layoutSubviews
{
[super layoutSubviews];
UIButton *aButton = (UIButton *)[self viewWithTag:100]; //get button
aButton.frame = CGRectMake(200, 5, 55, 40);
[aButton setTitle:@"tapMe" forState:UIControlStateNormal];

UILabel *label = (UILabel *) [self viewWithTag:200]; //get label
label.frame = CGRectMake(40, 5, 50, 35);
label.text = @"hello";

}

//hear is ur button's target
- (void)whenButtonTapped:(id)sender
{
//dont call UITableView delegate method in custom cell it is wrong, ur app get crashed
//insted create custom delegate method to your controller
[self.delegate whenButtonTapped:sender];//call the delegate method when button tapped
}

@end

//.h file where u are using custom cell

#import <UIKit/UIKit.h>
#import "CustomCell.h"
@interface ViewController : UIViewController<UITableViewDataSource , UITableViewDelegate ,CustomDelegate>//set confirms to delegate
@property (retain, nonatomic) IBOutlet UITableView *aTableView;

@end

//.m file

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *aCell = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
if(aCell == nil)
{
aCell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}
aCell.delegate = self;//set delegate to this class

return aCell;
}

//hear is your delegate method
//define your delegate method hear
-(void)whenButtonTapped:(UIButton *)sender
{

//in this method u can do what ever the activity when button tapped
//sender which gives u entire cell information
UITableViewCell *cell = sender.superview;//you can get cell also :)
NSLog(@"button tapped");

}


Hopes this helps

Distinguish between accessoryView and Row on touch

Observe a ; in your method signature even before body starts:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
}

As a side note from Apple Documentation:

Discussion The delegate usually responds to the tap on the disclosure
button (the accessory view) by displaying a new view related to the
selected row. This method is not called when an accessory view is set
for the row at indexPath.

This method is used for identifying touches on disclosure indicator and not on accessory view.

In order to support your accessory view touches, create custom UITableViewCelland add an UITapGestureRecognizer on your accessory view to intercept the taps.

accessoryButtonTappedForRow called on Simulator but not Device

Change if condition and break it into row and section separately.

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

// PubDisclaimer
if ((indexPath.row == 2 && indexPath.section == 2)) {
NSLog(@"Disclaimer Tapped");
//Show UIAlertController
}
// MaintenanceInfo
else if ((indexPath.row == 4 && indexPath.section == 0)) {
NSLog(@"Maintenance tapped");
//Show Different UIAlertController
}
else {
NSLog(@"Nothing tapped");
}
}

Hope this helps.



Related Topics



Leave a reply



Submit