Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Little refactoring in YXButtonCell
Browse files Browse the repository at this point in the history
  • Loading branch information
rbsgn committed Jul 19, 2010
1 parent d3a2a39 commit 25bc9b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
22 changes: 13 additions & 9 deletions Classes/YXButtonCell.h
Expand Up @@ -10,17 +10,21 @@
#import "YXAbstractCell.h"

@interface YXButtonCell : YXAbstractCell {
NSString * _title;
id _delegate;
SEL _selector;
@private
NSString * title_;
id target_;
SEL action_;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, copy, readonly) NSString * title;
@property (nonatomic, assign, readonly) id target;
@property (nonatomic, assign, readonly) SEL action;

// selector is a method like - (void)buttonTapped:(YXButtonCell*)cell;
+ (id)cellWithReuseIdentifier:(NSString*)reuseIdentifier withTitle:(NSString*)title
withDelegate:(id)delegate selector:(SEL)selector;
//
// Target must implement following action selector
// - (void)buttonTapped:(YXButtonCell*)cell;
//
+ (id)cellWithReuseIdentifier:(NSString *)reuseIdentifier title:(NSString *)title
target:(id)delegate action:(SEL)selector;

@end
51 changes: 34 additions & 17 deletions Classes/YXButtonCell.m
Expand Up @@ -8,32 +8,31 @@

#import "YXButtonCell.h"

@interface YXButtonCell ()

@implementation YXButtonCell
@property (nonatomic, copy, readwrite) NSString * title;
@property (nonatomic, assign, readwrite) id target;
@property (nonatomic, assign, readwrite) SEL action;

@synthesize title = _title;
@synthesize delegate = _delegate;
@synthesize selector = _selector;
@end

- (void)dealloc {
self.delegate = nil;
_selector = NULL;

self.title = nil;
@implementation YXButtonCell

[super dealloc];
}

+ (id)cellWithReuseIdentifier:(NSString*)reuseIdentifier withTitle:(NSString*)title
withDelegate:(id)delegate selector:(SEL)selector {
+ (id)cellWithReuseIdentifier:(NSString *)reuseIdentifier title:(NSString *)title
target:(id)target action:(SEL)action
{
YXButtonCell * cell = [[YXButtonCell alloc] initWithReuseIdentifier:reuseIdentifier];
cell.delegate = delegate;
cell.selector = selector;

cell.target = target;
cell.action = action;
cell.title = title;

return cell;
}

- (UITableViewCell*)tableViewCellWithReusableCell:(UITableViewCell*)reusableCell {
- (UITableViewCell*)tableViewCellWithReusableCell:(UITableViewCell *)reusableCell {
UITableViewCell * cell = reusableCell;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.reuseIdentifier] autorelease];
Expand All @@ -50,9 +49,27 @@ - (UITableViewCell*)tableViewCellWithReusableCell:(UITableViewCell*)reusableCell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];

if (_delegate != nil && _selector != NULL) {
[_delegate performSelector:_selector withObject:self];
if (self.target != nil && self.action != NULL) {
[self.target performSelector:self.action withObject:self];
}
}


#pragma mark -
#pragma mark Memory management


@synthesize title = title_;
@synthesize target = target_;
@synthesize action = action_;


- (void)dealloc {
target_ = nil;
action_ = NULL;
[title_ release];

[super dealloc];
}

@end
6 changes: 3 additions & 3 deletions Classes/YXModelTableViewsViewController.m
Expand Up @@ -58,9 +58,9 @@ - (void)viewDidLoad {

YXSection * sectionC = [[YXSection alloc] initWithHeader:@"Section C before" footer:@"Section C after"];
[sectionC addCell:[YXButtonCell cellWithReuseIdentifier:@"buttonCell"
withTitle:@"Button"
withDelegate:self
selector:@selector(buttonCellTapped:)]];
title:@"Button"
target:self
action:@selector(buttonCellTapped:)]];

[self setSections:[NSArray arrayWithObjects:sectionA, sectionB, sectionC, nil]];

Expand Down

0 comments on commit 25bc9b9

Please sign in to comment.