Skip to content

Commit

Permalink
Some refactoriings to avoid problems with reuse
Browse files Browse the repository at this point in the history
Bug fixed. thanks to @jairobjunior
  • Loading branch information
klevison committed Jun 3, 2014
1 parent 078bc5b commit 77bc4e7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

@optional

- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)section;

- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView sectionClosed:(NSInteger)section;
- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView selectedSectionAtIndex:(NSInteger)section;

@end

Expand All @@ -31,8 +29,7 @@
@property(weak, nonatomic) IBOutlet UIView *backgroundHeaderView;
@property(weak, nonatomic) IBOutlet UIView *overHeaderView;
@property(nonatomic, strong) KMAppearence *headerSectionAppearence;

- (void)toggleOpenWithUserAction:(BOOL)userAction;
@property UITapGestureRecognizer *tapGesture;

- (void)addOverHeaderSubView:(UIView *)view;

Expand Down
40 changes: 11 additions & 29 deletions KMAccordionTableViewController/Classes/Header/KMSectionHeaderView.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,16 @@
@implementation KMSectionHeaderView

- (void)awakeFromNib {

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(toggleOpen:)];
[self addGestureRecognizer:tapGesture];

_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(toggleOpen:)];
[self addGestureRecognizer:_tapGesture];
}

- (IBAction)toggleOpen:(id)sender {
[self toggleOpenWithUserAction:YES];
}

- (void)toggleOpenWithUserAction:(BOOL)userAction {

self.disclosureButton.selected = !self.disclosureButton.selected;

if (userAction) {
if (self.disclosureButton.selected) {
if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionOpened:)]) {
[self.delegate sectionHeaderView:self sectionOpened:self.section];
}
}
else {
if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionClosed:)]) {
[self.delegate sectionHeaderView:self sectionClosed:self.section];
}
}
}

- (IBAction)toggleOpen:(id)sender
{
[self.delegate sectionHeaderView:self selectedSectionAtIndex:self.section];
}

- (void)prepareForReuse {
Expand All @@ -51,16 +33,16 @@ - (void)addOverHeaderSubView:(UIView *)view {
}

- (void)setHeaderSectionAppearence:(KMAppearence *)headerSectionAppearence {

_headerSectionAppearence = headerSectionAppearence;

self.headerSeparatorView.backgroundColor = headerSectionAppearence.headerSeparatorColor;
self.backgroundHeaderView.backgroundColor = headerSectionAppearence.headerColor;
self.titleLabel.font = headerSectionAppearence.headerFont;
self.titleLabel.textColor = headerSectionAppearence.headerTitleColor;
[self.disclosureButton setImage:headerSectionAppearence.headerArrowImageOpened forState:UIControlStateNormal];
[self.disclosureButton setImage:headerSectionAppearence.headerArrowImageClosed forState:UIControlStateSelected];

}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -166,53 +166,65 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger

#pragma mark - SectionHeaderViewDelegate

- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)sectionOpened {

- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView selectedSectionAtIndex:(NSInteger)sectionOpened
{

KMSection *section = (self.sections)[sectionOpened];

if (!section.open) {
[self sectionHeaderView:sectionHeaderView sectionOpened:sectionOpened];
}else{
[self sectionHeaderView:sectionHeaderView sectionClosed:sectionOpened];
}

}

- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)sectionOpened {

KMSection *section = (self.sections)[sectionOpened];

section.open = YES;

NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:0 inSection:sectionOpened]];

NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];

NSInteger previousOpenSectionIndex = self.openSectionIndex;

if (previousOpenSectionIndex != NSNotFound) {
KMSection *previousOpenSection = (self.sections)[previousOpenSectionIndex];
previousOpenSection.open = NO;
[previousOpenSection.headerView toggleOpenWithUserAction:NO];
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:0 inSection:previousOpenSectionIndex]];
}

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

CGRect sectionRect = [self.tableView rectForSection:sectionOpened];
[self.tableView scrollRectToVisible:sectionRect animated:YES];

self.openSectionIndex = sectionOpened;

}

- (void)sectionHeaderView:(KMSectionHeaderView *)sectionHeaderView sectionClosed:(NSInteger)sectionClosed {

KMSection *currentSection = (self.sections)[sectionClosed];

currentSection.open = NO;
NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:sectionClosed];

if (countOfRowsToDelete > 0) {
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:0 inSection:sectionClosed]];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationFade];
}

self.openSectionIndex = NSNotFound;

}

@end
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ DEPENDENCIES:
- Reveal-iOS-SDK

SPEC CHECKSUMS:
Reveal-iOS-SDK: 9de027a2cb0812e9ae220d9047161f3a0ac2db74
Reveal-iOS-SDK: d3c8e109d42219daaa7a6e1ad2de34f0bdbb7a88

COCOAPODS: 0.32.1
COCOAPODS: 0.33.1

0 comments on commit 77bc4e7

Please sign in to comment.