Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide or remove section from uitableview

i am implementing a grouped table view in my application, where user can select row from different section of table.

i want to hide(remove) particular section(including header) depending upon selection of row of first section.

i.e.

first sections header is "Do you own car?", answer would be YES or NO in row selection. if user selects NO, second and third section from grouped table should hide(remove).

for radio selection in grouped table, i implement this

i also refer this but does not full fill what i need.

Please help.

Edit

TableViewDelegates

myArray is my datasource. myArrayAnswerCount contains count of number of row per section

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [myArray count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    for (int i = 0; i<[myArray count]; i++) {
        if (section==i) {
            return [[myArray objectAtIndex:i] title];
        }
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    for (int i = 0; i<[myArray count]; i++) {
        if (section==i) {
            return [[myArrayAnswerCount objectAtIndex:i] intValue];
        }
    }
}

this is code for how i removes section from table, myIndexPath is NSIndexPath variable which is pointing to section which is currently selected.

[table beginUpdates];
[myArray removeObjectAtIndex:myIndexPath.section];
[table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:YES];
[table endUpdates];
like image 461
iCoder86 Avatar asked Jun 27 '11 07:06

iCoder86


1 Answers

I solved my question.

[table beginUpdates];
[myArray removeObjectAtIndex:myIndexPath.section];
[table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:UITableViewRowAnimationRight];
[table endUpdates];

The issue in "invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (3)" error was i forgot to remove object from myArrayAnswerCount array too.

so finally following code

[table beginUpdates];
[myArray removeObjectAtIndex:myIndexPath.section];
[myArrayAnswerCount removeObjectAtIndex:myIndexPath.section]; //this is what i forgot.
[table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:UITableViewRowAnimationRight];
[table endUpdates];

thanks.

like image 117
iCoder86 Avatar answered Oct 12 '22 14:10

iCoder86