Skip to content

Commit

Permalink
Changed code to use UIButtons for dictionary lookup and all table cel…
Browse files Browse the repository at this point in the history
…ls now use the custom EntryTableCell
  • Loading branch information
Jamie Ly committed Oct 21, 2012
1 parent b8cd512 commit 368641b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Jumbly/Controllers/PuzzleViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#import "Jumble.h"

@interface PuzzleViewController : UIViewController <UITableViewDataSource,
UITableViewDelegate, UITextFieldDelegate>
UITableViewDelegate, UITextFieldDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) Jumble *jumble;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)onNewPuzzle:(id)sender;
- (IBAction)onShowSolution:(id)sender;
- (IBAction)onQuit:(id)sender;
@property (strong, nonatomic) IBOutlet UITapGestureRecognizer *gestureRecognizer;
@end
59 changes: 40 additions & 19 deletions Jumbly/Controllers/PuzzleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@interface PuzzleViewController () {
Chain *chain;
NSArray *guesses;
UIImage *defineImage;
}
@end

Expand All @@ -24,6 +25,9 @@ @implementation PuzzleViewController
- (void)viewDidLoad
{
[super viewDidLoad];
defineImage = [UIImage imageNamed:@"define.png"];
[self.gestureRecognizer addTarget:self action:@selector(onDefine:)];
self.gestureRecognizer.delegate = self;
}

- (void) viewDidAppear:(BOOL)animated {
Expand Down Expand Up @@ -97,36 +101,42 @@ - (NSString*) wordBeforeIndexPath: (NSIndexPath*) ip {

- (UITableViewCell*) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
EntryTableCell *cell = cell = [tableView dequeueReusableCellWithIdentifier:@"EntryTableCell"];

if(chain) {
NSInteger row = indexPath.row;
if(![self isGuessRow: indexPath]) {
cell = [tableView dequeueReusableCellWithIdentifier:@"StandardTableCell"];
cell.textLabel.text = [chain wordAtIndex: indexPath.row];
cell.word = [chain wordAtIndex: indexPath.row];
cell.infoButton.tag = indexPath.row;
[cell.infoButton addTarget:self action:@selector(onDefine:) forControlEvents:UIControlEventTouchUpInside];
cell.infoButton.hidden = NO;
cell.locked = YES;
}
else {
NSInteger guessRow = row - 1;
Guess *guess = [guesses objectAtIndex: guessRow];

cell = [tableView dequeueReusableCellWithIdentifier:@"EntryTableCell"];
//cell.textLabel.text = @"?";
EntryTableCell *entryCell = (EntryTableCell *) cell;
entryCell.textField.delegate = self;
cell.textField.delegate = self;

NSString *previousWord = [self wordBeforeIndexPath: indexPath];

entryCell.validWord = [self wordIsDefined: guess.word] &&
cell.validWord = [self wordIsDefined: guess.word] &&
[jumble word: guess.word isNeighborOf: previousWord];

entryCell.textField.text = guess.word;

[self bindTextField: entryCell.textField toGuess: guess];
cell.word = guess.word;
cell.infoButton.hidden = YES;
if([self wordIsDefined: guess.word]) {
cell.infoButton.tag = indexPath.row;
[cell.infoButton addTarget:self action:@selector(onDefine:) forControlEvents:UIControlEventTouchUpInside];
cell.infoButton.hidden = NO;
}
cell.locked = NO;
[self bindTextField: cell.textField toGuess: guess];
}
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:@"StandardTableCell"];
cell.textLabel.text = @"Puzzle could not be generated.";
cell.word = @"Generating...";
cell.locked = YES;
}

return cell;
Expand All @@ -150,14 +160,10 @@ - (BOOL) isGuessRow: (NSIndexPath*) ip {
return !(row == 0 || row == chain.count - 1);
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(!chain) return;

NSString *word = [self wordAtIndexPath: indexPath];

- (void) showDefinition: (NSString*) word {
if([self wordIsDefined: word]) {
UIReferenceLibraryViewController * controller =
[[UIReferenceLibraryViewController alloc] initWithTerm: word];
[[UIReferenceLibraryViewController alloc] initWithTerm: word];
[self presentViewController: controller animated: YES completion:^{
// ?
}];
Expand Down Expand Up @@ -186,6 +192,13 @@ - (IBAction)onQuit:(id)sender {
}];
}

- (void)onDefine:(id)sender {
NSInteger row = [sender tag];
NSString *word = [self wordAtIndexPath: [NSIndexPath indexPathForItem:row
inSection:0]];
[self showDefinition: word];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField.text isEqualToString: @"?"]) {
textField.text = @"";
Expand All @@ -209,4 +222,12 @@ - (BOOL) textFieldShouldReturn:(UITextField *)textField {
return YES;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}

@end
3 changes: 3 additions & 0 deletions Jumbly/Views/EntryTableCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#import <UIKit/UIKit.h>

@interface EntryTableCell : UITableViewCell
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, copy) NSString* word;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, assign) BOOL validWord;
@property (nonatomic, strong) UIButton *infoButton;
@property (nonatomic, assign) BOOL locked;
@end
32 changes: 29 additions & 3 deletions Jumbly/Views/EntryTableCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ @implementation EntryTableCell
@synthesize textField;
@synthesize validWord;
@synthesize infoButton;
@synthesize textLabel;
@synthesize locked;
@synthesize word;

- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder: aDecoder];
Expand Down Expand Up @@ -45,18 +48,28 @@ - (void) initialize: (CGRect) frame {
frame.size.height -= padding*2;
frame.size.width -= horizontalPadding*2;
textField = [[UITextField alloc] initWithFrame: frame];

textField.textAlignment = NSTextAlignmentCenter;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]];
[self addSubview: textField];

textLabel = [[UILabel alloc] initWithFrame: frame];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]];
textLabel.hidden = YES;
textLabel.backgroundColor = [UIColor clearColor];
[self addSubview: textLabel];

self.selectedBackgroundView = nil;
self.backgroundView = nil;
self.selectionStyle = UITableViewCellSeparatorStyleNone;

self.imageView.image = [UIImage imageNamed:@"define.png"];
self.imageView.userInteractionEnabled = YES;
infoButton = [UIButton buttonWithType: UIButtonTypeInfoDark];
CGRect buttonFrame = infoButton.frame;
buttonFrame.origin = CGPointMake(padding, frame.size.height / 2.0f);
infoButton.frame = buttonFrame;
[self.contentView addSubview: infoButton];
}

- (void) setValidWord:(BOOL)aValidWord {
Expand All @@ -65,4 +78,17 @@ - (void) setValidWord:(BOOL)aValidWord {
UITableViewCellAccessoryNone;
}

- (void) setLocked: (BOOL) value {
locked = value;
textField.hidden = locked;
textLabel.hidden = !locked;
self.backgroundColor = locked ? [UIColor yellowColor] : [UIColor whiteColor];
}

- (void) setWord:(NSString *)aWord {
word = aWord;
textField.text = word;
textLabel.text = word;
}

@end
7 changes: 7 additions & 0 deletions Jumbly/en.lproj/MainStoryboard_iPhone.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
</view>
<connections>
<outlet property="activityIndicator" destination="Do3-2h-hBd" id="g4e-WG-3a2"/>
<outlet property="gestureRecognizer" destination="XeU-dz-XSc" id="rRH-tr-4nW"/>
<outlet property="tableView" destination="jNW-km-9Mc" id="Hxh-Ts-NBU"/>
</connections>
</viewController>
Expand All @@ -177,6 +178,11 @@
<rect key="frame" x="0.0" y="0.0" width="37" height="37"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</activityIndicatorView>
<tapGestureRecognizer id="XeU-dz-XSc">
<connections>
<outlet property="delegate" destination="DQf-Kc-Rgr" id="QoT-G2-yCV"/>
</connections>
</tapGestureRecognizer>
</objects>
<point key="canvasLocation" x="836" y="136"/>
</scene>
Expand All @@ -195,6 +201,7 @@
<relationship kind="action" name="onQuit:"/>
<relationship kind="action" name="onShowSolution:"/>
<relationship kind="outlet" name="activityIndicator" candidateClass="UIActivityIndicatorView"/>
<relationship kind="outlet" name="gestureRecognizer" candidateClass="UITapGestureRecognizer"/>
<relationship kind="outlet" name="tableView" candidateClass="UITableView"/>
</relationships>
</class>
Expand Down

0 comments on commit 368641b

Please sign in to comment.