Hide or remove section from uitableview

I implement the presentation of a grouped table in my application, where the user can select a row from another section of the table.

I want to hide (delete) a specific section (including the title) depending on the row selection of the first section.

i.e.

the heading of the first sections is “Do you have a car?”, the answer will be YES or NO in the row selection. if the user selects NO, the second and third sections from the grouped table should hide (delete).

to select a radio in a grouped table, I implement this

I also refer to this one , but do not fill in everything that I need.

Please, help.

Edit

TableViewDelegates

myArray is my data source. myArrayAnswerCount contains the number of rows in the 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 the code for how I delete the section from the table, myIndexPath is the NSIndexPath variable that points to the selected section.

 [table beginUpdates]; [myArray removeObjectAtIndex:myIndexPath.section]; [table deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:YES]; [table endUpdates]; 
+6
source share
4 answers

I solved my question.

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

The problem is "the number of rows in section 0 is invalid. The number of lines contained in the existing section after update (2) must be equal to the number of lines contained in this section before the update (3) error" I also forgot to remove the object from the myArrayAnswerCount array.

so finally the 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.

+9
source

You watched this demo provided by APPLE.

Hope this helps.

+1
source

You can check your UITableViewDataSource as:

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView{ if(YOUR_INSTANCE_BOOL_VAR==NO){ return 1; }else{ return 3; } } 

After the user selects NO, turn your YOUR_INSTANCE_BOOL_VAR to NO, then type [tableView reloadData] ; Obviously, you also need to manipulate numberOfRowsInSection to call up the number of rows you want after the user selects NO.

Another way is to call: - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

You can get indexPaths strings with:

 + (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section; 
0
source

If you are not trying to delete a section or row, just hide it. You can use:

 [tableView beginUpdates]; [tableView reloadSections:[NSindexSet indexSetWithIndex:(indexOfSection)] withRowAnimation:(UITableViewRowAnimationOfChoice)]; //hide section 

OR

 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationOfChoice)]; //hide rows [tableView endUpdates]; 

If you read the documentation, it says that they behave similarly to the delete / insert methods, but they are not actually deleted, so there is no need to delete / insert. You can call this method two hidden or hidden.

0
source

Source: https://habr.com/ru/post/891425/


All Articles