Creating an accordion in an UITableView (iOS)

Creating an accordion in an UITableView (iOS)

This example shows a hidden UIWebView in the TableViewCell.

Add in the following to the header file
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
@property (strong, nonatomic) NSIndexPath *indexPathSelected;
@property (strong, nonatomic) NSIndexPath *selectedIndexPath;

I did not have any time to update the dutch labels and variables but the compiling errors will show straightaway what variables should be changed 😉
[cc lang=”objc” escaped=”true” width=”90%” height=”100%” theme=”default” line_numbers=”on”]
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

if ([indexPath compare:_selectedIndexPath] == NSOrderedSame) {

return 165;
}
else {

}
return 64;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *clickedNow = indexPath;
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
bool alreayopen= NO;
if ([clickedNow isEqual:_selectedIndexPath]) {
//already open
alreayopen = YES;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:clickedNow];
UIImageView *plaatje = (UIImageView *)[cell.contentView viewWithTag:10];
plaatje.image = [UIImage imageNamed:@”Openen.png”];
UILabel *antwoord = (UILabel *)[cell.contentView viewWithTag:2];
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFade;
animation.duration = 0.4;
[antwoord.layer addAnimation:animation forKey:nil];
UIWebView *LabelToUpdate = (UIWebView *)[cell.contentView viewWithTag:120];
_selectedIndexPath = nil;
}
else {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:clickedNow];
UIImageView *plaatje = (UIImageView *)[cell.contentView viewWithTag:10];
plaatje.image = [UIImage imageNamed:@”Sluiten.png”];
UILabel *antwoord = (UILabel *)[cell.contentView viewWithTag:2];
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFade;
animation.duration = 0.4;
[antwoord.layer addAnimation:animation forKey:nil];

UIWebView *antwoordOpDeVraag = (UIWebView *)[cell.contentView viewWithTag:120];
antwoordOpDeVraag.hidden = NO;
[antwoordOpDeVraag.layer addAnimation:animation forKey:nil];

if (_selectedIndexPath == nil){

}
else {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:_selectedIndexPath];
UIImageView *plaatje = (UIImageView *)[cell.contentView viewWithTag:10];
plaatje.image = [UIImage imageNamed:@”Openen.png”];
UILabel *antwoord = (UILabel *)[cell.contentView viewWithTag:2];

CATransition *animation = [CATransition animation];
animation.type = kCATransitionFade;
animation.duration = 0.4;
[antwoord.layer addAnimation:animation forKey:nil];

UIWebView *antwoordOpDeVraag = (UIWebView *)[cell.contentView viewWithTag:120];
antwoordOpDeVraag.hidden = YES;
[antwoordOpDeVraag.layer addAnimation:animation forKey:nil];

}

_selectedIndexPath = clickedNow;
}

//Committing the changes and provoking the heightForRowAtIndexPath delegate
[self.tableView beginUpdates];
[self.tableView endUpdates];

}
}