The correct way is to use the datasource to everything related to specific cell's view. This is an example of implementation of click and expand cells.
In fact for iOS developers this should be one of most often used patterns.
@interface MySitesViewController ()
@property (strong, nonatomic) NSMutableArray *menuRows;
@end
@implementation MySitesViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_menuRows = [NSMutableArray array];
[_menuRows addObject:@{
@"title":"Some title",
@"type": @"MyCellType",
@"height":@(44)
}.mutableCopy];
[_menuRows addObject:@{
@"title":"Some title",
@"type": @"MyCellType",
@"height":@(44)
}.mutableCopy];
[_menuRows addObject:@{
@"title":"Some title",
@"type": @"MyCellType",
@"height":@(44)
}.mutableCopy];
[_menuRows addObject:@{
@"title":"Some title",
@"type": @"MyCellType",
@"height":@(44)
}.mutableCopy];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_menuRows count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = _menuRows[indexPath.row];
return data[@"height"];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *data = _menuRows[indexPath.row];
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:data[@"type"]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = _menuRows[indexPath.row];
[tableView deselectRowAtIndexPath:indexPath animated: YES];
NSInteger height = ((NSNumber*)data[@"height"]).integerValue;
if(height == 44)
{
data[@"height"] = @(200);
}else
{
data[@"height"] = @(44);
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end