Wednesday, March 24, 2010

Refactoring Suggested Items: Standard Callback Handler

The standard callback handler


One of the other items that I created is a standard callback handler. This is responsible for taking standard actions from the table, and converting them into semantic actions on my data model.


The interface


The interface is pretty standard, note that JLCallbackHandler implements the UITableViewDelegate protocol.  This will have a reference to a JLTableController object, and know how to call it when actions occur.  In addition it has a reference to the JLTableSource so that it can retrieve selected items and act upon them.


//  JLStandardCallbackHandler.h
//  JLFoundation
//
//  Created by Jon Lundy on 3/20/10.
//

#import "JLTableControl.h"
#import "JLStandardCallbackHandler.h"

@interface JLStandardCallbackHandler : NSObject {
id<JLTableController> tableController;
id<JLTableSource> source;
}

@end

The implementation


The implementation is very simple, but it helps abstract out these tasks and enables more complicated behavior in the future.  It takes the given index path, asks the source for the data at that path, and then dispatches to the proper callback in the table controller.



//
//  JLStandardCallbackHandler.m
//  JLFoundation
//
//  Created by Jon Lundy on 3/20/10.
//

#import "JLStandardCallbackHandler.h"


@implementation JLStandardCallbackHandler
@synthesize tableController;
@synthesize source;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
id itemAtIndex=[source objectAtIndexPath:indexPath];
[tableController rowSelected:itemAtIndex];
}

- (void)tableView: (UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
id itemAtIndex=[source objectAtIndexPath:indexPath];
[tableController acessorySelected:itemAtIndex];

}



@end

No comments:

Post a Comment