This isn't working as expected. The App becomes completely unresponsive and freezes as soon as I attempt to press the UIButton in the sectionHeaderView.
The button works when it it's target method returns nil so it's definitely the method i have set up as it's target/action.
Where am I going wrong here?
The button works when it it's target method returns nil so it's definitely the method i have set up as it's target/action.
Where am I going wrong here?
Code:
@interface ListViewController ()
@property (nonatomic, strong) NSMutableArray *tableViewIndexPaths;
@property (nonatomic, strong) NSMutableArray *toggleFlags;
@end
@implementation VAListViewController {
NSInteger sectionButton;
}
@synthesize tableViewIndexPaths = _tableViewIndexPaths;
@synthesize toggleFlags = _toggleFlags;
-(NSMutableArray*)tableViewIndexPaths
{
if (_tableViewIndexPaths == nil) {
_tableViewIndexPaths = [[NSMutableArray alloc] init];
}
return _tableViewIndexPaths;
}
-(NSMutableArray*)toggleFlags
{
if (_toggleFlags == nil) {
_toggleFlags = [[NSMutableArray alloc] init];
}
return _toggleFlags;
}
-(UIButton*)generateExpandCollapseButtonForHeaderView // Called and frame set in viewForHeaderInSection
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = sectionButton++;
[button addTarget:self action:@selector(sectionExpandCollapseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
[self.tableViewIndexPaths addObject:indexPath];
BOOL isSectionExpanded = NO;
[self.toggleFlags addObject:[NSNumber numberWithBool:isSectionExpanded]];
...........some other stuff
}
-(void)sectionExpandCollapseButtonPressed:(UIButton*)sender
{
if (![sender isKindOfClass:[UIButton class]]) {
NSLog(@"Sender is not a button");
}
NSMutableArray *tmpArray = [NSMutableArray array];
NSInteger rows = [self.tableView numberOfRowsInSection:sender.tag];
int i;
BOOL toggleFlag = [[self.toggleFlags objectAtIndex:sender.tag] boolValue];
BOOL isSectionExpanded = !toggleFlag;
[self.toggleFlags removeObjectAtIndex:sender.tag];
[self.toggleFlags insertObject:[NSNumber numberWithBool:isSectionExpanded] atIndex:sender.tag];
if (isSectionExpanded)
{
for (i=0; i<rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:sender.tag];
[tmpArray addObject:tmpIndexPath];
}
[self.tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
} else
{
for (i=0; i<rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:sender.tag];
[tmpArray addObject:tmpIndexPath];
}
[self.tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
}