diff --git a/ExpandTableView/JKExpandTableView.h b/ExpandTableView/JKExpandTableView.h index 69ac40b..5199a19 100644 --- a/ExpandTableView/JKExpandTableView.h +++ b/ExpandTableView/JKExpandTableView.h @@ -8,6 +8,9 @@ #import #import "JKMultiSelectSubTableViewCell.h" +#import "JKSingleSelectSubTableViewCell.h" +#import "JKParentTableViewCell.h" +#import "JKSubTableViewCellCell.h" /*! @protocol JKExpandTableViewDelegate @@ -24,6 +27,9 @@ - (BOOL) shouldSupportMultipleSelectableChildrenAtParentIndex:(NSInteger) parentIndex; @optional + +- (BOOL)singleChoiceBehavior; + /*! Optional method the delegate should implement to get notified when a child is clicked on. @param childIndex The child index in question @@ -42,6 +48,19 @@ */ - (void) tableView:(UITableView *)tableView didSelectParentCellAtIndex:(NSInteger) parentIndex; +/*! Optional method to override and provide your custom cell for multi selection mode. + + */ +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView multiSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex; +/*! Optional method to override and provide your custom cell for single selection mode. + + */ +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView singleSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex; +/*! Optional method to override and provide your custom cell for parent cells. + + */ +- (JKParentTableViewCell *)tableView:(UITableView *)tableView parentCellForRowAtIndexPath:(NSIndexPath *)indexPath; + /*! Optional method to set custom foreground color. @return UIColor @@ -106,6 +125,12 @@ - (BOOL) shouldDisplaySelectedStateForCellAtChildIndex:(NSInteger) childIndex withinParentCellIndex:(NSInteger) parentIndex; @optional +/*! Optional method + */ +- (CGFloat)heightForParentCell; +/*! Optional method + */ +- (CGFloat)heightForChildCell; /*! Optional method diff --git a/ExpandTableView/JKExpandTableView.m b/ExpandTableView/JKExpandTableView.m index 7f86d6d..0f415db 100644 --- a/ExpandTableView/JKExpandTableView.m +++ b/ExpandTableView/JKExpandTableView.m @@ -7,14 +7,18 @@ // #import "JKExpandTableView.h" -#import "JKParentTableViewCell.h" -#import "JKMultiSelectSubTableViewCell.h" -#import "JKSingleSelectSubTableViewCell.h" + +@interface JKExpandTableView () + +@property (nonatomic, assign) NSInteger lastExpandedPosition; + +@end @implementation JKExpandTableView @synthesize tableViewDelegate, expansionStates; -#define HEIGHT_FOR_CELL 44.0 +#define HEIGHT_FOR_PARENT_CELL 44.0 +#define HEIGHT_FOR_CHILD_CELL 44.0 - (id)initWithFrame:(CGRect)frame dataSource:dataDelegate tableViewDelegate:tableDelegate { self = [super initWithFrame:frame style:UITableViewStylePlain]; @@ -52,6 +56,7 @@ - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableVi */ - (void) initialize { + self.lastExpandedPosition = -1; [self setDataSource:self]; [self setDelegate:self]; self.separatorStyle = UITableViewCellSeparatorStyleSingleLine; @@ -69,6 +74,11 @@ - (void) setDataSourceDelegate:(id) deleg { [self initExpansionStates]; } +- (void)reloadData { + [self initExpansionStates]; + [super reloadData]; +} + - (void) initExpansionStates { // all collapsed initially @@ -84,10 +94,33 @@ - (void) expandForParentAtRow: (NSInteger) row { if ([[self.expansionStates objectAtIndex:parentIndex] boolValue]) { return; } + // update expansionStates so backing data is ready before calling insertRowsAtIndexPaths [self.expansionStates replaceObjectAtIndex:parentIndex withObject:@"YES"]; [self insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(row + 1) inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; + + [self collapsePreviousRow:row]; +} + +- (void)collapsePreviousRow:(NSInteger)currentExpandedRow { + // used to solve bug when the last expanded position is after current position + BOOL offset = self.lastExpandedPosition > [self parentIndexForRow:currentExpandedRow]; + if ([self.tableViewDelegate respondsToSelector:@selector(singleChoiceBehavior)] + && [self.tableViewDelegate singleChoiceBehavior] + && self.lastExpandedPosition != -1 + && currentExpandedRow != self.lastExpandedPosition) { + NSInteger pos = offset? self.lastExpandedPosition + 1 : self.lastExpandedPosition; + [self collapseForParentAtRow:pos]; + + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:pos inSection:0]; + UITableViewCell *selectedCell = [self cellForRowAtIndexPath:indexPath]; + if ([selectedCell isKindOfClass:[JKParentTableViewCell class]]) { + JKParentTableViewCell * pCell = (JKParentTableViewCell *)selectedCell; + [self animateParentCellIconExpand:NO forCell:pCell]; + } + } + self.lastExpandedPosition = [self parentIndexForRow:currentExpandedRow]; } - (void) collapseForParentAtRow: (NSInteger) row { @@ -189,6 +222,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N BOOL isMultiSelect = [self.tableViewDelegate shouldSupportMultipleSelectableChildrenAtParentIndex:parentIndex]; if (isMultiSelect) { JKMultiSelectSubTableViewCell *cell = (JKMultiSelectSubTableViewCell *)[self dequeueReusableCellWithIdentifier:CellIdentifier_MultiSelect]; + if (cell == nil) { cell = [[JKMultiSelectSubTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier_MultiSelect]; } else { @@ -210,6 +244,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N } NSLog(@"cellForRowAtIndexPath MultiSelect parentIndex: %ld", (long)parentIndex); + [cell setChildCellHeight:[self heightForChildCell]]; [cell setParentIndex:parentIndex]; [cell setDelegate:self]; [cell reload]; @@ -242,14 +277,22 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N } NSLog(@"cellForRowAtIndexPath SingleSelect parentIndex: %ld", (long)parentIndex); + [cell setChildCellHeight:[self heightForChildCell]]; [cell setParentIndex:parentIndex]; [cell setDelegate:self]; [cell reload]; return cell; } } else { + JKParentTableViewCell *cell = nil; + if ([self.tableViewDelegate respondsToSelector:@selector(tableView:parentCellForRowAtIndexPath:)]) { + NSInteger parentRow = [self parentIndexForRow:indexPath.row]; + cell = [self.tableViewDelegate tableView:tableView parentCellForRowAtIndexPath:[NSIndexPath indexPathForRow:parentRow inSection:0]]; + } // regular parent cell - JKParentTableViewCell *cell = (JKParentTableViewCell *)[self dequeueReusableCellWithIdentifier:CellIdentifier_Parent]; + if (cell == nil) { + cell = (JKParentTableViewCell *)[self dequeueReusableCellWithIdentifier:CellIdentifier_Parent]; + } if (cell == nil) { cell = [[JKParentTableViewCell alloc] initWithReuseIdentifier:CellIdentifier_Parent]; } else { @@ -285,7 +328,6 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N [cell setParentIndex:parentIndex]; [cell selectionIndicatorState:[self hasSelectedChild:parentIndex]]; - //[cell setupDisplay]; return cell; } @@ -300,9 +342,9 @@ -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPat if (isExpansionCell) { NSInteger parentIndex = [self parentIndexForRow:row]; NSInteger numberOfChildren = [self.dataSourceDelegate numberOfChildCellsUnderParentIndex:parentIndex]; - return HEIGHT_FOR_CELL * numberOfChildren; + return [self heightForChildCell] * numberOfChildren; } else { - return HEIGHT_FOR_CELL; + return [self heightForParentCell]; } } @@ -332,6 +374,39 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath } #pragma mark - JKMultiSelectSubTableViewCellDelegate + +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView multiSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex { + JKSubTableViewCellCell *cell; + if ([self.tableViewDelegate respondsToSelector:@selector(tableView:multiSelectCellForRowAtIndexPath:withInParentCellIndex:)]) { + cell = [self.tableViewDelegate tableView:tableView multiSelectCellForRowAtIndexPath:indexPath withInParentCellIndex:parentIndex]; + } + return cell; +} + +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView singleSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex { + JKSubTableViewCellCell *cell; + if ([self.tableViewDelegate respondsToSelector:@selector(tableView:singleSelectCellForRowAtIndexPath:withInParentCellIndex:)]) { + cell = [self.tableViewDelegate tableView:tableView singleSelectCellForRowAtIndexPath:indexPath withInParentCellIndex:parentIndex]; + } + return cell; +} + +- (CGFloat)heightForParentCell { + CGFloat heightForParentCell = HEIGHT_FOR_PARENT_CELL; + if ([self.dataSourceDelegate respondsToSelector:@selector(heightForParentCell)]) { + heightForParentCell = [self.dataSourceDelegate heightForParentCell]; + } + return heightForParentCell; +} + +- (CGFloat)heightForChildCell { + CGFloat heightForChildCell = HEIGHT_FOR_CHILD_CELL; + if ([self.dataSourceDelegate respondsToSelector:@selector(heightForChildCell)]) { + heightForChildCell = [self.dataSourceDelegate heightForChildCell]; + } + return heightForChildCell; +} + - (NSInteger) numberOfChildrenUnderParentIndex:(NSInteger)parentIndex { return [self.dataSourceDelegate numberOfChildCellsUnderParentIndex:parentIndex]; } diff --git a/ExpandTableView/JKMultiSelectSubTableViewCell.m b/ExpandTableView/JKMultiSelectSubTableViewCell.m index 987f0c7..af9c9b4 100644 --- a/ExpandTableView/JKMultiSelectSubTableViewCell.m +++ b/ExpandTableView/JKMultiSelectSubTableViewCell.m @@ -27,7 +27,18 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath } [self.delegate didSelectRowAtChildIndex:indexPath.row selected:isSwitchedOn underParentIndex:self.parentIndex]; + + [tableView deselectRowAtIndexPath:indexPath animated:YES]; } +- (JKSubTableViewCellCell *)customChildCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex { + JKSubTableViewCellCell *cell; + if ([self.delegate tableView:tableView multiSelectCellForRowAtIndexPath:indexPath withInParentCellIndex:parentIndex] != nil) { + cell = [self.delegate tableView:tableView multiSelectCellForRowAtIndexPath:indexPath withInParentCellIndex:parentIndex]; + } else { + cell = [super customChildCell:tableView indexPath:indexPath withInParentCellIndex:parentIndex]; + } + return cell; +} @end diff --git a/ExpandTableView/JKParentTableViewCell.h b/ExpandTableView/JKParentTableViewCell.h index 3c0ae2e..8f09914 100644 --- a/ExpandTableView/JKParentTableViewCell.h +++ b/ExpandTableView/JKParentTableViewCell.h @@ -29,4 +29,5 @@ - (void)setCellForegroundColor:(UIColor *) foregroundColor; - (void)setCellBackgroundColor:(UIColor *) backgroundColor; +- (UIImageView *)pIndicator; @end diff --git a/ExpandTableView/JKParentTableViewCell.m b/ExpandTableView/JKParentTableViewCell.m index 27303dc..1a56fa6 100644 --- a/ExpandTableView/JKParentTableViewCell.m +++ b/ExpandTableView/JKParentTableViewCell.m @@ -80,17 +80,21 @@ - (void)setupDisplay { checkMarkHeight); } +- (UIImageView *)icon { + return [self pIndicator] != nil ? [self pIndicator] : iconImage; +} + - (void)rotateIconToExpanded { [UIView beginAnimations:@"rotateDisclosure" context:nil]; [UIView setAnimationDuration:0.2]; - iconImage.transform = CGAffineTransformMakeRotation(M_PI * 2.5); + [self icon].transform = CGAffineTransformMakeRotation(M_PI * 2.5); [UIView commitAnimations]; } - (void)rotateIconToCollapsed { [UIView beginAnimations:@"rotateDisclosure" context:nil]; [UIView setAnimationDuration:0.2]; - iconImage.transform = CGAffineTransformMakeRotation(M_PI * 2); + [self icon].transform = CGAffineTransformMakeRotation(M_PI * 2); [UIView commitAnimations]; } @@ -107,6 +111,11 @@ - (void)selectionIndicatorState:(BOOL) visible { } } +// subclasses can implement +- (UIImageView *)pIndicator { + return nil; +} + - (void)setCellForegroundColor:(UIColor *) foregroundColor { self.label.textColor = foregroundColor; } diff --git a/ExpandTableView/JKSingleSelectSubTableViewCell.m b/ExpandTableView/JKSingleSelectSubTableViewCell.m index b824b85..b17712d 100644 --- a/ExpandTableView/JKSingleSelectSubTableViewCell.m +++ b/ExpandTableView/JKSingleSelectSubTableViewCell.m @@ -42,6 +42,18 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath } [self.delegate didSelectRowAtChildIndex:indexPath.row selected:isSwitchedOn underParentIndex:self.parentIndex]; + + [tableView deselectRowAtIndexPath:indexPath animated:YES]; +} + +- (JKSubTableViewCellCell *)customChildCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex { + JKSubTableViewCellCell *cell; + if ([self.delegate tableView:tableView singleSelectCellForRowAtIndexPath:indexPath withInParentCellIndex:parentIndex] != nil) { + cell = [self.delegate tableView:tableView singleSelectCellForRowAtIndexPath:indexPath withInParentCellIndex:parentIndex]; + } else { + cell = [super customChildCell:tableView indexPath:indexPath withInParentCellIndex:parentIndex]; + } + return cell; } @end diff --git a/ExpandTableView/JKSubTableViewCell.h b/ExpandTableView/JKSubTableViewCell.h index a8491b9..2f76fd7 100644 --- a/ExpandTableView/JKSubTableViewCell.h +++ b/ExpandTableView/JKSubTableViewCell.h @@ -8,6 +8,8 @@ #import +@class JKSubTableViewCellCell; + @protocol JKSubTableViewCellDelegate // return total number of children under this parentIndex - (NSInteger) numberOfChildrenUnderParentIndex:(NSInteger)parentIndex; @@ -24,9 +26,20 @@ - (NSString *) labelForChildIndex:(NSInteger)childIndex underParentIndex:(NSInteger)parentIndex; // get the icon image - (UIImage *) iconForChildIndex:(NSInteger)childIndex underParentIndex:(NSInteger)parentIndex; + +@optional +/*! Optional method to override and provide your custom cell for multi selection mode. + + */ +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView multiSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex; +/*! Optional method to override and provide your custom cell for single selection mode. + + */ +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView singleSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex; + @end -@interface JKSubTableViewCell : UITableViewCell { +@interface JKSubTableViewCell : UITableViewCell { UITableView *insideTableView; __weak id delegate; UIColor *bgColor; @@ -38,12 +51,16 @@ @property(nonatomic,strong) UITableView *insideTableView; @property(nonatomic,weak,getter = getDelegate, setter = setDelegate:) id delegate; @property(nonatomic) NSInteger parentIndex; +@property(nonatomic, assign) CGFloat childCellHeight; @property(nonatomic,strong) UIImage *selectionIndicatorImg; + @property(nonatomic,strong,getter = getSubTableForegroundColor, setter = setSubTableForegroundColor:) UIColor *fgColor; @property(nonatomic,strong,getter = getSubTableBackgroundColor, setter = setSubTableBackgroundColor:) UIColor *bgColor; @property(nonatomic,strong,getter = getSubTableFont, setter = setSubTableFont:) UIFont *font; +- (void)customInit; +- (JKSubTableViewCellCell *)customChildCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger)parentIndex; - (UIImage *) selectionIndicatorImgOrDefault; - (void) reload; diff --git a/ExpandTableView/JKSubTableViewCell.m b/ExpandTableView/JKSubTableViewCell.m index 86fcfbc..9267f12 100644 --- a/ExpandTableView/JKSubTableViewCell.m +++ b/ExpandTableView/JKSubTableViewCell.m @@ -8,34 +8,37 @@ #import "JKSubTableViewCell.h" #import "JKSubTableViewCellCell.h" +#import "JKExpandTableView.h" @implementation JKSubTableViewCell @synthesize insideTableView, selectionIndicatorImg; -#define HEIGHT_FOR_CELL 44.0 - - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { - self.insideTableView = [[UITableView alloc] init]; - insideTableView.dataSource = self; - insideTableView.delegate = self; - [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - [[self contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - [self.insideTableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - insideTableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height); - fgColor = [UIColor darkTextColor]; - bgColor = [UIColor clearColor]; - font = [UIFont systemFontOfSize:16.0]; - insideTableView.backgroundColor = [UIColor clearColor]; - insideTableView.scrollEnabled = NO; - [self.contentView addSubview:self.insideTableView]; + [self customInit]; } return self; } +- (void)customInit { + self.insideTableView = [[UITableView alloc] init]; + insideTableView.dataSource = self; + insideTableView.delegate = self; + [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; + [[self contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; + [self.insideTableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; + insideTableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height); + fgColor = [UIColor darkTextColor]; + bgColor = [UIColor clearColor]; + font = [UIFont systemFontOfSize:16.0]; + insideTableView.backgroundColor = [UIColor clearColor]; + insideTableView.scrollEnabled = NO; + [self.contentView addSubview:self.insideTableView]; +} + - (id) getDelegate { return delegate; } @@ -44,7 +47,8 @@ - (id) getDelegate { - (void) setDelegate:(id)deleg { delegate = deleg; NSInteger numberOfChild = [delegate numberOfChildrenUnderParentIndex:self.parentIndex]; - insideTableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, HEIGHT_FOR_CELL * numberOfChild); + + insideTableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.childCellHeight * numberOfChild); } - (void)setSelected:(BOOL)selected animated:(BOOL)animated @@ -87,6 +91,11 @@ - (void) reload { [self.insideTableView reloadData]; } +// subclasses should implement. +- (JKSubTableViewCellCell *)customChildCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger)parentIndex { + return nil; +} + #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { @@ -100,7 +109,14 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"SubTableViewCellCell_Reuse_Id"; - JKSubTableViewCellCell *cell = (JKSubTableViewCellCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + JKSubTableViewCellCell *cell; + BOOL isCustomCell = NO; + if ([self customChildCell:tableView indexPath:indexPath withInParentCellIndex:self.parentIndex] != nil) { + cell = [self customChildCell:tableView indexPath:indexPath withInParentCellIndex:self.parentIndex]; + isCustomCell = YES; + } else if (cell == nil) { + cell = (JKSubTableViewCellCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + } if (cell == nil) { cell = [[JKSubTableViewCellCell alloc] initWithReuseIdentifier:CellIdentifier]; } else { @@ -108,35 +124,36 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N } NSInteger row = [indexPath row]; - cell.titleLabel.text = [self.delegate labelForChildIndex:row underParentIndex:self.parentIndex]; - cell.iconImage.image = [self.delegate iconForChildIndex:row underParentIndex:self.parentIndex]; - cell.selectionIndicatorImg.image = [self selectionIndicatorImgOrDefault]; - - BOOL isRowSelected = [self.delegate isSelectedForChildIndex:row underParentIndex:self.parentIndex]; - - if (isRowSelected) { - cell.selectionIndicatorImg.hidden = NO; - } else { - cell.selectionIndicatorImg.hidden = YES; + if (!isCustomCell) { + cell.titleLabel.text = [self.delegate labelForChildIndex:row underParentIndex:self.parentIndex]; + cell.iconImage.image = [self.delegate iconForChildIndex:row underParentIndex:self.parentIndex]; + cell.selectionIndicatorImg.image = [self selectionIndicatorImgOrDefault]; + + BOOL isRowSelected = [self.delegate isSelectedForChildIndex:row underParentIndex:self.parentIndex]; + + if (isRowSelected) { + cell.selectionIndicatorImg.hidden = NO; + } else { + cell.selectionIndicatorImg.hidden = YES; + } + + [cell setCellBackgroundColor:bgColor]; + [cell setCellForegroundColor:fgColor]; + [cell.titleLabel setFont:font]; + + cell.selectionStyle = UITableViewCellSelectionStyleNone; + //cell.textLabel.textColor = [UIColor grayColor]; + cell.textLabel.font = [UIFont systemFontOfSize:16]; + //[cell setupDisplay]; } - - [cell setCellBackgroundColor:bgColor]; - [cell setCellForegroundColor:fgColor]; - [cell.titleLabel setFont:font]; - - cell.selectionStyle = UITableViewCellSelectionStyleNone; - //cell.textLabel.textColor = [UIColor grayColor]; - cell.textLabel.font = [UIFont systemFontOfSize:16]; - //[cell setupDisplay]; return cell; } - #pragma mark - Table view delegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - return HEIGHT_FOR_CELL; + return self.childCellHeight; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples.xcodeproj/project.pbxproj b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples.xcodeproj/project.pbxproj index f6bfc32..d8b0401 100644 --- a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples.xcodeproj/project.pbxproj +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples.xcodeproj/project.pbxproj @@ -7,6 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + 061691A81AD99B7000B36315 /* CustomMultiSelectCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 061691A61AD99B7000B36315 /* CustomMultiSelectCell.m */; }; + 061691AB1AD99F8400B36315 /* CustomMultiSelectCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 061691AA1AD99F8400B36315 /* CustomMultiSelectCell.xib */; }; + 061691AE1ADABE6200B36315 /* CustomParentCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 061691AD1ADABE6200B36315 /* CustomParentCell.m */; }; + 061691B01ADABE7500B36315 /* CustomParentCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 061691AF1ADABE7500B36315 /* CustomParentCell.xib */; }; + 061FD71D1AD40B0E0015E9CD /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7E3102E17A0D9340049FD09 /* CoreGraphics.framework */; }; B7C7C7D417A21C160046DDCE /* cat.png in Resources */ = {isa = PBXBuildFile; fileRef = B7C7C7D317A21C160046DDCE /* cat.png */; }; B7C7C7D617A21C1F0046DDCE /* heart.png in Resources */ = {isa = PBXBuildFile; fileRef = B7C7C7D517A21C1F0046DDCE /* heart.png */; }; B7D377F317A327400001F5BC /* SimpleExampleViewControllerPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = B7D377F217A327400001F5BC /* SimpleExampleViewControllerPad.xib */; }; @@ -64,6 +69,12 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 061691A51AD99B7000B36315 /* CustomMultiSelectCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomMultiSelectCell.h; sourceTree = ""; }; + 061691A61AD99B7000B36315 /* CustomMultiSelectCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomMultiSelectCell.m; sourceTree = ""; }; + 061691AA1AD99F8400B36315 /* CustomMultiSelectCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CustomMultiSelectCell.xib; sourceTree = ""; }; + 061691AC1ADABE6200B36315 /* CustomParentCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomParentCell.h; sourceTree = ""; }; + 061691AD1ADABE6200B36315 /* CustomParentCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomParentCell.m; sourceTree = ""; }; + 061691AF1ADABE7500B36315 /* CustomParentCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CustomParentCell.xib; sourceTree = ""; }; B7C7C7D317A21C160046DDCE /* cat.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cat.png; sourceTree = ""; }; B7C7C7D517A21C1F0046DDCE /* heart.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = heart.png; sourceTree = ""; }; B7D377F217A327400001F5BC /* SimpleExampleViewControllerPad.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SimpleExampleViewControllerPad.xib; sourceTree = ""; }; @@ -82,7 +93,7 @@ B7E3103C17A0D9340049FD09 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = ""; }; B7E3103E17A0D9340049FD09 /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = ""; }; B7E3104017A0D9340049FD09 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; - B7E3104717A0D9340049FD09 /* JKExpandTableViewSamplesTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JKExpandTableViewSamplesTests.octest; sourceTree = BUILT_PRODUCTS_DIR; }; + B7E3104717A0D9340049FD09 /* JKExpandTableViewSamplesTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JKExpandTableViewSamplesTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; B7E3105017A0D9350049FD09 /* JKExpandTableViewSamplesTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "JKExpandTableViewSamplesTests-Info.plist"; sourceTree = ""; }; B7E3105217A0D9350049FD09 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; B7E3105417A0D9350049FD09 /* JKExpandTableViewSamplesTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JKExpandTableViewSamplesTests.h; sourceTree = ""; }; @@ -125,6 +136,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 061FD71D1AD40B0E0015E9CD /* CoreGraphics.framework in Frameworks */, B7E3104A17A0D9350049FD09 /* UIKit.framework in Frameworks */, B7E3104B17A0D9350049FD09 /* Foundation.framework in Frameworks */, ); @@ -148,7 +160,7 @@ isa = PBXGroup; children = ( B7E3102717A0D9340049FD09 /* JKExpandTableViewSamples.app */, - B7E3104717A0D9340049FD09 /* JKExpandTableViewSamplesTests.octest */, + B7E3104717A0D9340049FD09 /* JKExpandTableViewSamplesTests.xctest */, ); name = Products; sourceTree = ""; @@ -174,6 +186,12 @@ B7D377F217A327400001F5BC /* SimpleExampleViewControllerPad.xib */, B7E3108117A0D9E80049FD09 /* examples_img */, B7E3103117A0D9340049FD09 /* Supporting Files */, + 061691A51AD99B7000B36315 /* CustomMultiSelectCell.h */, + 061691A61AD99B7000B36315 /* CustomMultiSelectCell.m */, + 061691AA1AD99F8400B36315 /* CustomMultiSelectCell.xib */, + 061691AC1ADABE6200B36315 /* CustomParentCell.h */, + 061691AD1ADABE6200B36315 /* CustomParentCell.m */, + 061691AF1ADABE7500B36315 /* CustomParentCell.xib */, ); path = JKExpandTableViewSamples; sourceTree = ""; @@ -300,7 +318,7 @@ ); name = JKExpandTableViewSamplesTests; productName = JKExpandTableViewSamplesTests; - productReference = B7E3104717A0D9340049FD09 /* JKExpandTableViewSamplesTests.octest */; + productReference = B7E3104717A0D9340049FD09 /* JKExpandTableViewSamplesTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; }; /* End PBXNativeTarget section */ @@ -347,9 +365,11 @@ B7E3103F17A0D9340049FD09 /* Default@2x.png in Resources */, B7E3104117A0D9340049FD09 /* Default-568h@2x.png in Resources */, B7E3108317A0D9E80049FD09 /* green_checkmark.png in Resources */, + 061691B01ADABE7500B36315 /* CustomParentCell.xib in Resources */, B7D7223517A1DC8C005680A5 /* green_checkmark@2x.png in Resources */, B7D7223617A1DC8C005680A5 /* smartphone.png in Resources */, B7C7C7D417A21C160046DDCE /* cat.png in Resources */, + 061691AB1AD99F8400B36315 /* CustomMultiSelectCell.xib in Resources */, B7C7C7D617A21C1F0046DDCE /* heart.png in Resources */, B7D377F317A327400001F5BC /* SimpleExampleViewControllerPad.xib in Resources */, B7FB1C9017A779C500078022 /* dog.png in Resources */, @@ -392,12 +412,14 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 061691AE1ADABE6200B36315 /* CustomParentCell.m in Sources */, B7E3108917A0DB7F0049FD09 /* JKExpandTableView.m in Sources */, B7E3108B17A0DB7F0049FD09 /* JKMultiSelectSubTableViewCell.m in Sources */, B7E3108D17A0DB7F0049FD09 /* JKParentTableViewCell.m in Sources */, B7E3108F17A0DB7F0049FD09 /* JKSingleSelectSubTableViewCell.m in Sources */, B7E3109117A0DB7F0049FD09 /* JKSubTableViewCell.m in Sources */, B7E3109317A0DB7F0049FD09 /* JKSubTableViewCellCell.m in Sources */, + 061691A81AD99B7000B36315 /* CustomMultiSelectCell.m in Sources */, B7E3108617A0DA4B0049FD09 /* SimpleExampleViewController.m in Sources */, B7E3103717A0D9340049FD09 /* main.m in Sources */, B7E3103B17A0D9340049FD09 /* JKAppDelegate.m in Sources */, diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.h b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.h new file mode 100644 index 0000000..63b1d82 --- /dev/null +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.h @@ -0,0 +1,14 @@ +// +// CutomMultiSelectCell.h +// JKExpandTableViewSamples +// +// Created by Jesse A on 4/11/15. +// Copyright (c) 2015 Jack Kwok. All rights reserved. +// + +#import +#import "JKSubTableViewCellCell.h" + +@interface CustomMultiSelectCell : JKSubTableViewCellCell + +@end diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.m b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.m new file mode 100644 index 0000000..079899b --- /dev/null +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.m @@ -0,0 +1,17 @@ +// +// CutomMultiSelectCell.m +// JKExpandTableViewSamples +// +// Created by Jesse A on 4/11/15. +// Copyright (c) 2015 Jack Kwok. All rights reserved. +// + +#import "CustomMultiSelectCell.h" + +@interface CustomMultiSelectCell () + +@end + +@implementation CustomMultiSelectCell + +@end diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.xib b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.xib new file mode 100644 index 0000000..61c49e5 --- /dev/null +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomMultiSelectCell.xib @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.h b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.h new file mode 100644 index 0000000..4f39a55 --- /dev/null +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.h @@ -0,0 +1,15 @@ +// +// CustomParentCell.h +// JKExpandTableViewSamples +// +// Created by Jesse A on 4/12/15. +// Copyright (c) 2015 Jack Kwok. All rights reserved. +// + +#import "JKParentTableViewCell.h" + +@interface CustomParentCell : JKParentTableViewCell + +- (UIImageView *)pIndicator; + +@end diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.m b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.m new file mode 100644 index 0000000..7382dfd --- /dev/null +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.m @@ -0,0 +1,22 @@ +// +// CustomParentCell.m +// JKExpandTableViewSamples +// +// Created by Jesse A on 4/12/15. +// Copyright (c) 2015 Jack Kwok. All rights reserved. +// + +#import "CustomParentCell.h" + +@interface CustomParentCell () + +@property (nonatomic,strong) IBOutlet UIImageView *indicator; +@end + +@implementation CustomParentCell + +- (UIImageView *)pIndicator { + return self.indicator; +} + +@end diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.xib b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.xib new file mode 100644 index 0000000..01e15be --- /dev/null +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/CustomParentCell.xib @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/SimpleExampleViewController.m b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/SimpleExampleViewController.m index 5832784..e67e910 100644 --- a/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/SimpleExampleViewController.m +++ b/Samples/JKExpandTableViewSamples/JKExpandTableViewSamples/SimpleExampleViewController.m @@ -7,6 +7,8 @@ // #import "SimpleExampleViewController.h" +#import "CustomMultiSelectCell.h" +#import "CustomParentCell.h" @interface SimpleExampleViewController () @@ -114,6 +116,16 @@ - (NSInteger) numberOfParentCells { return [self.dataModelArray count]; } +// OPTIONAL, you don't have to specify this. +- (CGFloat)heightForParentCell { + return 44.0f; +} + +// OPTIONAL, you don't have to specify this. +- (CGFloat)heightForChildCell { + return 64.0f; +} + - (NSInteger) numberOfChildCellsUnderParentIndex:(NSInteger) parentIndex { NSMutableArray *childArray = [self.dataModelArray objectAtIndex:parentIndex]; return [childArray count]; @@ -136,6 +148,34 @@ - (UIImage *) iconForParentCellAtIndex:(NSInteger) parentIndex { return [UIImage imageNamed:@"arrow-icon"]; } +// OPTIONAL, you don't have to specify this. +- (JKSubTableViewCellCell *)tableView:(UITableView *)tableView multiSelectCellForRowAtIndexPath:(NSIndexPath *)indexPath withInParentCellIndex:(NSInteger) parentIndex { + CustomMultiSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomMultiSelectCell class])]; + if (!cell) { + NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CustomMultiSelectCell class]) owner:self options:nil]; + cell = [nib objectAtIndex:0]; + } + return cell; +} + +// OPTIONAL, you don't have to specify this. +- (JKParentTableViewCell *)tableView:(UITableView *)tableView parentCellForRowAtIndexPath:(NSIndexPath *)indexPath { + CustomParentCell *cell = nil; + if (indexPath.row == self.dataModelArray.count - 1) { + cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomParentCell class])]; + if (!cell) { + NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CustomParentCell class]) owner:self options:nil]; + cell = [nib objectAtIndex:0]; + } + } + return cell; +} + +// optional, default is NO +- (BOOL)singleChoiceBehavior { + return YES; +} + - (UIImage *) iconForCellAtChildIndex:(NSInteger) childIndex withinParentCellIndex:(NSInteger) parentIndex { if (((childIndex + parentIndex) % 3) == 0) { return [UIImage imageNamed:@"heart"];