Skip to content

Commit

Permalink
Merge pull request #17 from benzguo/daz/control_bug_fixes
Browse files Browse the repository at this point in the history
Made improvements so keyboard controls work during scrolling
  • Loading branch information
0thernet committed Apr 10, 2014
2 parents 1730452 + 60205fc commit c6098ed
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions BZGFormViewController.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = 'BZGFormViewController'
s.version = '2.2.0'
s.version = '2.3.1'
s.license = 'MIT'
s.summary = 'A library for creating dynamic forms.'
s.homepage = 'https://github.com/benzguo/BZGFormViewController'
s.author = { 'Ben Guo' => '[email protected]' }
s.source = {
:git => 'https://github.com/benzguo/BZGFormViewController.git',
:tag => 'v2.2.0'
:tag => "v#{s.version}"
}
s.dependency 'ReactiveCocoa', '~>2.2.4'
s.dependency 'libextobjc', '~>0.4'
Expand Down
58 changes: 44 additions & 14 deletions BZGFormViewController/BZGFormViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface BZGFormViewController ()
@property (nonatomic, assign) UITableViewStyle style;
@property (nonatomic, assign) BOOL isValid;
@property (nonatomic, strong) BZGKeyboardControl *keyboardControl;
@property (nonatomic, copy) void (^didEndScrollingBlock)();

@end

Expand Down Expand Up @@ -52,7 +53,7 @@ - (void)loadView
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
}
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = BZG_TABLEVIEW_BACKGROUND_COLOR;

Expand Down Expand Up @@ -109,7 +110,7 @@ - (void)showInfoCellBelowFormCell:(BZGTextFieldCell *)cell
{
NSUInteger cellIndex = [self.formCells indexOfObject:cell];
if (cellIndex == NSNotFound) return;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellIndex+1
inSection:self.formSection];

Expand Down Expand Up @@ -144,10 +145,10 @@ - (void)updateInfoCellBelowFormCell:(BZGTextFieldCell *)cell
if (!cell.textField.editing &&
(cell.validationState == BZGValidationStateInvalid ||
cell.validationState == BZGValidationStateWarning)) {
[self showInfoCellBelowFormCell:cell];
} else {
[self removeInfoCellBelowFormCell:cell];
}
[self showInfoCellBelowFormCell:cell];
} else {
[self removeInfoCellBelowFormCell:cell];
}
}

#pragma mark - Finding cells
Expand Down Expand Up @@ -182,7 +183,7 @@ - (BZGTextFieldCell *)previousFormCell:(BZGTextFieldCell *)cell
{
NSUInteger cellIndex = [self.formCells indexOfObject:cell];
if (cellIndex == NSNotFound || cellIndex == 0) return nil;

for (NSInteger i = cellIndex - 1; i >= 0; --i) {
UITableViewCell *cell = self.formCells[i];
if ([cell isKindOfClass:[BZGTextFieldCell class]]) {
Expand Down Expand Up @@ -230,9 +231,9 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField
if (cell.didBeginEditingBlock) {
cell.didBeginEditingBlock(cell, textField.text);
}

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
if (self.showsKeyboardControl) {
[self accesorizeTextField:textField];
}
Expand Down Expand Up @@ -264,7 +265,7 @@ - (void)textFieldDidEndEditing:(UITextField *)textField
if (cell.didEndEditingBlock) {
cell.didEndEditingBlock(cell, textField.text);
}

[self updateInfoCellBelowFormCell:cell];
}

Expand Down Expand Up @@ -300,8 +301,8 @@ - (void)formCell:(BZGFormCell *)formCell didChangeValidationState:(BZGValidation
for (BZGFormCell *cell in self.formCells) {
if ([cell isKindOfClass:[BZGFormCell class]]) {
isValid = isValid &&
(cell.validationState == BZGValidationStateValid ||
cell.validationState == BZGValidationStateWarning);
(cell.validationState == BZGValidationStateValid ||
cell.validationState == BZGValidationStateWarning);
}
}
self.isValid = isValid;
Expand Down Expand Up @@ -353,15 +354,44 @@ - (BZGKeyboardControl *)keyboardControl {
}

- (void)navigateToPreviousCell: (id)sender {
[self.keyboardControl.previousCell.textField becomeFirstResponder];
BZGTextFieldCell *previousCell = self.keyboardControl.previousCell;
[self navigateToDestinationCell:previousCell];
}

- (void)navigateToNextCell {
[self.keyboardControl.nextCell.textField becomeFirstResponder];
BZGTextFieldCell *nextCell = self.keyboardControl.nextCell;
[self navigateToDestinationCell:nextCell];
}

- (void)navigateToDestinationCell:(BZGTextFieldCell *)destinationCell {
[self.keyboardControl.currentCell resignFirstResponder];
if ([[self.tableView visibleCells] containsObject:destinationCell]) {
[destinationCell.textField becomeFirstResponder];
}
else {
NSUInteger row = [self.formCells indexOfObject:destinationCell];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:self.formSection];
self.didEndScrollingBlock = ^{
[destinationCell.textField becomeFirstResponder];
};
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}

- (void)doneButtonPressed {
[self.keyboardControl.currentCell.textField resignFirstResponder];
}

#pragma mark - UIScrollView Methods

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
if (scrollView == self.tableView) {
if (self.didEndScrollingBlock) {
self.didEndScrollingBlock();
self.didEndScrollingBlock = nil;
}
}
}

@end

0 comments on commit c6098ed

Please sign in to comment.