Getting the accessoryButtonTappedForRowWithIndexPath: to get called.

The accessoryButtonTappedForRowWithIndexPath: is a delegate method from the UITableViewDelegate Protocol.  

Compiling and running on iPhone OS 2.2.1 the accessoryButtonTappedForRowWithIndexPath: method gets called as expected and compiling and running on iPhone 3.0 works as expected. 

However, an issues arrise with compiling for iPhone OS 2.2.1 and running on iPhone 3.0.  Here's what to do:


Step by step:

1. Declare your controller class uses conforms to the UITableViewDelegate Protocol

    @interface MyViewController : UITableViewController <UITableViewDelegate>

2. When creating table cells set the correct attributues to enable a detail disclosure button and clear out the cell's target!

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{

    static NSString *CellIdentifier = @"My_Cell";

    TableViewCell *cell = (TableViewCell *)[MyTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[TableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    // Need to set the cells' target to nil or else accessoryButtonTappedForRowWithIndexPath: will not be called.

    cell.target = nil;

    return cell;

}


The UITableView looks to see if a custom target has been defined, if so it will call the custom target instead of accessoryButtonTappedForRowWithIndexPath: delegate method.  In the process of running the 2.2.1 code on 3.0, the target get's assigned garbage, so simply setting it to nil after alloc'ing it, fixes the problem.


Copyright © 2008-2012 Irongeek Software, LLC.  support@irongeeksoftware.com