Created
April 5, 2012 03:04
-
-
Save amudi/2307663 to your computer and use it in GitHub Desktop.
Snippet of AccordionTableView logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@synthesize selectedIndexPath; | |
@synthesize defaultRowHeight; | |
@synthesize optionRowHeight; | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
self.optionRowHeight = 40; | |
} | |
return self; | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
self.selectedIndexPath = nil; | |
} | |
#pragma mark - UITableViewDelegate | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (self.selectedIndexPath && self.selectedIndexPath.row + 1 == indexPath.row) { | |
return optionRowHeight; | |
} | |
return defaultRowHeight; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (indexPath.section == self.numberOfSections) { | |
return; | |
} | |
if (self.selectedIndexPath && self.selectedIndexPath.row + 1 == indexPath.row) { | |
return; | |
} | |
[tableView beginUpdates]; | |
if ([self.selectedIndexPath isEqual:indexPath]) { | |
self.selectedIndexPath = nil; | |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]] | |
withRowAnimation:UITableViewRowAnimationBottom]; | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
} else { | |
NSIndexPath *oldSelectedIndexPath = self.selectedIndexPath; | |
if (oldSelectedIndexPath) { | |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: | |
[NSIndexPath indexPathForRow:oldSelectedIndexPath.row + 1 | |
inSection:oldSelectedIndexPath.section]] | |
withRowAnimation:UITableViewRowAnimationBottom]; | |
[tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:oldSelectedIndexPath.row | |
inSection:oldSelectedIndexPath.section] | |
animated:YES]; | |
} | |
if (!oldSelectedIndexPath || | |
[oldSelectedIndexPath compare:indexPath] == NSOrderedDescending) { | |
// newly selected row above old selection | |
self.selectedIndexPath = indexPath; | |
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject: | |
[NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1 | |
inSection:indexPath.section]] | |
withRowAnimation:UITableViewRowAnimationBottom]; | |
} else { | |
// newly selected row below old selection | |
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject: | |
[NSIndexPath indexPathForRow:indexPath.row | |
inSection:indexPath.section]] | |
withRowAnimation:UITableViewRowAnimationBottom]; | |
self.selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]; | |
} | |
[tableView scrollToRowAtIndexPath:self.selectedIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES]; | |
} | |
[tableView endUpdates]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment