Hide or remove section from uitableview

16,486

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.

Share:
16,486
iCoder86
Author by

iCoder86

iOS Developer

Updated on June 18, 2022

Comments

  • iCoder86
    iCoder86 almost 2 years

    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];
    
  • iCoder86
    iCoder86 almost 13 years
    @Mat : Hi, i have choose to use - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; method, now how can i update my data(i mean NSMutableArray), that are loaded in grouped table, after successful removel of particular section from group?
  • Mat
    Mat almost 13 years
    I don't know what type of objects you have in your NSMutableArray and in what order, but with NSMutableArray you can add/remove/replace any objects by accessing the index.
  • iCoder86
    iCoder86 almost 13 years
    @Mat : hi, i got some error "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)" while deleting section 0. is it mandatory that section 1 must have equal numbers of row to section 0 while deleting section?
  • Mat
    Mat almost 13 years
    This is a classic problem, probably because reloadData is called before you modify the dataSource, but if you edit your question and post your tableviewdelegate and tableviewdatasource callbacks maybe we can better understand where is the problem.
  • iCoder86
    iCoder86 almost 13 years
    @Mat : Hi, i solved issue, actually i forget to remove object from myArrayAnswerCount. that's why it was showing me such error. thanks for help..
  • Eshwar Chaitanya
    Eshwar Chaitanya over 12 years
    Is myArrayAnswerCount the value of [myArray count] or some other value.If I give [[myArray Count] removeObjectAtIndex:],it is throwing a warning "method removeObjectAtIndex" not found,please help me,thanks in advance :)
  • iCoder86
    iCoder86 over 12 years
    @Eshwar Chaitanya: myArrayAnswerCount contains number of row per section in table. It is obvious a mutable array.
  • Eshwar Chaitanya
    Eshwar Chaitanya over 12 years
    I have only 1 row per section and the number of sections vary when ever I add a reminder in add reminder page,please suggest me accordingly,thanks in advance :)
  • Eshwar Chaitanya
    Eshwar Chaitanya over 12 years
    What I mean is when I press edit,I am able to press the - sign and delete the cell,but again when I navigate back and enter the view where we perform edit and delete actions,the deleted cell is still available,how to make a section delete permanently,Thanks :)
  • iCoder86
    iCoder86 over 12 years
    @EshwarChaitanya: myArrayAnswerCount is just a mutable array which contains row count for each section in table (it would be an int count, in your case its value might be 1 for each section). including that you also need to remove value from data source (you might have use an mutable array for that). first check that. also debug your code accordingly.
  • Eshwar Chaitanya
    Eshwar Chaitanya over 12 years
    ,Hi If you don't mind and when ever you find free time,please go through this link once,please stackoverflow.com/questions/8708543/… Thanks for everything and Happy new year :)