Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reload tableview's particular section

I have 56 sections in my tableview, and I am storing the section number which is currently selected in an integer variable named "activeTimeSlot". How to use the below method to reload a section which is currently selected. I am doing it like

 [self.tblView reloadSections:[NSIndexSet indexSetWithIndex:activeTimeSlot] withRowAnimation:UITableViewRowAnimationAutomatic];

but I have observed that doing this way is calling the below datasource method of tableview for all the sections, i.e its reloading all the section.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

I am not sure how to use NSIndexSet and how to use NSIndexSet to reload more than 1 sections.

like image 221
Vishal Singh Avatar asked May 01 '13 07:05

Vishal Singh


2 Answers

If you want to update multiple section then you can do this way.

NSMutableIndexSet *indetsetToUpdate = [[NSMutableIndexSet alloc]init];

[indetsetToUpdate addIndex:previousSection]; // [indetsetToUpdate addIndex:<#(NSUInteger)#>] 
// You can add multiple indexes(sections) here.

[detailTableView reloadSections:indetsetToUpdate withRowAnimation:UITableViewRowAnimationFade];

This is used for updating multiple sections and it's working fine for me.

like image 189
Nirav Jain Avatar answered Nov 02 '22 00:11

Nirav Jain


As I saw from your comments, you are checking the call of the method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

to figure out that it's reloading every section. But that method is called for all the sections for internal reasons probably. If you try to log which rows are actually realoaded, you'll see that everything is fine. put a

NSLog(@"Section %d", indexPath.section);

in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

and see that just rows from the selected section are reloaded.

PS: forgot to say: your solution was already correct.

like image 20
Nicola Miotto Avatar answered Nov 02 '22 00:11

Nicola Miotto