Permalink
Browse files

first refactoring done

  • Loading branch information...
1 parent 538c130 commit 22cf45b5478c6da5ddcbc255895d6b3a80c99c7a @oscardelben committed Mar 31, 2011
@@ -37,20 +37,31 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
cell.delegate = self;
- cell.doneSelector = @selector(doneAction);
+ cell.doneSelector = @selector(doneAction:atIndexPath:);
- cell.rightButtonTextNormal = @"Normal";
- cell.rightButtonTextSelected = @"Tapped";
- cell.rightButtonTextDone = @"Done";
- cell.textLabel.text = [NSString stringWithFormat:@"Cell %i", indexPath.row + 1];
+ UIButton *normalButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ normalButton.frame = CGRectMake(0, 0, 80, 25);
+ [normalButton setTitle:@"Normal" forState:UIControlStateNormal];
+
+ UIButton *selectedButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ selectedButton.frame = CGRectMake(0, 0, 80, 25);
+ [selectedButton setTitle:@"Selected" forState:UIControlStateNormal];
+
+ UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ doneButton.frame = CGRectMake(0, 0, 80, 25);
+ [doneButton setTitle:@"Done" forState:UIControlStateNormal];
+
+ cell.buttonStateNormal = normalButton;
+ cell.buttonStateSelected = selectedButton;
+ cell.buttonStateDone = doneButton;
+
+ cell.textLabel.text = [NSString stringWithFormat:@"Cell %i", indexPath.row];
return cell;
}
-
-// To do, should pass the cell itself and indexPath
-- (void)doneAction
+- (void)doneAction:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
- NSLog(@"Done");
+ NSLog(@"Done. Row: %i", indexPath.row);
}
@end
@@ -14,10 +14,6 @@
@property (nonatomic, retain) id delegate;
-@property (nonatomic, retain) NSString *rightButtonTextNormal;
-@property (nonatomic, retain) NSString *rightButtonTextSelected;
-@property (nonatomic, retain) NSString *rightButtonTextDone;
-
@property (nonatomic, retain) NSArray *buttons;
@property (nonatomic, retain) UIButton *buttonStateNormal;
@@ -18,7 +18,6 @@
@implementation CustomCell
@synthesize delegate;
-@synthesize rightButtonTextNormal, rightButtonTextSelected, rightButtonTextDone;
@synthesize rightButtonState;
@synthesize buttons;
@synthesize buttonStateNormal, buttonStateSelected, buttonStateDone;
@@ -27,9 +26,6 @@ @implementation CustomCell
- (void)dealloc
{
[delegate release];
- [rightButtonTextNormal release];
- [rightButtonTextSelected release];
- [rightButtonTextDone release];
[buttons release];
@@ -42,68 +38,55 @@ - (void)dealloc
- (void)switchViewsFromState:(int)fromState toState:(int)toState
{
- UIView *view1 = [buttons objectAtIndex:fromState];
- UIView *view2 = [buttons objectAtIndex:toState];
+ UIButton *button1 = [buttons objectAtIndex:fromState];
+ UIButton *button2 = [buttons objectAtIndex:toState];
rightButtonState = toState;
- view1.hidden = YES;
- view2.hidden = NO;
+ button1.hidden = YES;
+ button2.hidden = NO;
}
-- (UIButton *)buttonWithTitle:(NSString *)title;
+- (void)configureButtons:(NSArray *)views
{
- float textWidth = [title sizeWithFont:[UIFont boldSystemFontOfSize:fontSize]].width;
-
- int width = textWidth + padding * 2;
- int height = self.frame.size.height - padding * 2;
-
- int x = self.frame.size.width - width - padding * 2;
- int y = padding;
-
- CGRect buttonRect = CGRectMake(x, y, width, height);
-
- UIButton *button = [[[UIButton alloc] initWithFrame:buttonRect] autorelease];
-
- [button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
-
- button.backgroundColor = [UIColor orangeColor];
-
- [button setTitle:title forState:UIControlStateNormal];
- button.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
-
- // hidden by default
- button.hidden = YES;
-
- return button;
+ for (UIButton *button in views)
+ {
+ // calculate frame
+ int width = button.frame.size.width;
+ int height = button.frame.size.height;
+
+ int x = self.frame.size.width - width - padding * 2;
+ int y = (self.frame.size.height - height) / 2;
+
+ button.frame = CGRectMake(x, y, width, height);
+ button.hidden = YES;
+
+ [button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
+ }
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
- // We already have the buttons
- if (buttons)
+ // Check if we already have the buttons
+ if (self.buttons)
return;
-
- buttonStateNormal = [self buttonWithTitle:rightButtonTextNormal];
- buttonStateSelected = [self buttonWithTitle:rightButtonTextSelected];
- buttonStateDone = [self buttonWithTitle:rightButtonTextDone];
self.buttons = [NSArray arrayWithObjects:buttonStateNormal, buttonStateSelected, buttonStateDone, nil];
+ [self configureButtons:buttons];
// Should set this if null (but we don't have null for integers right?). Maybe it's already 0 by default
- rightButtonState = kRightButtonStateNormal;
+ self.rightButtonState = kRightButtonStateNormal;
UIButton *button = (UIButton *)[buttons objectAtIndex:rightButtonState];
button.hidden = NO;
-
+
[self addSubview:buttonStateNormal];
[self addSubview:buttonStateSelected];
[self addSubview:buttonStateDone];
}
-// TODO: add callbacks for changing state
- (void)buttonTouched:(id)sender
{
switch (rightButtonState)
@@ -119,7 +102,8 @@ - (void)buttonTouched:(id)sender
case kRightButtonStateSelected:
[self switchViewsFromState:kRightButtonStateSelected toState:kRightButtonStateDone];
if (delegate) {
- [delegate performSelector:doneSelector];
+ NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
+ [delegate performSelector:doneSelector withObject:self withObject:indexPath];
}
break;
}
View
@@ -1,3 +1,9 @@
UITableViewCell with right button that detects touches. Similar to the one in the App Store
-Work in progress.
+Work in progress. Almost done.
+
+Todo:
+
+* Refactor code to add more consistency
+* Add animations
+* Documentation

0 comments on commit 22cf45b

Please sign in to comment.