Update sections in UITableView

I am writing an application that notifies the user when it is time to take his medicine. The label at the top of the page shows the date, and the tableView filled with the names of the drugs and the time you need to take on that particular day. Sections are now populated based on the amount of medication you need to take on that day. Thus, the number of sections dynamically changes over time; the medicine is planned to be taken on this day. Whenever the user takes the medicine, it is saved in the database as a variable that changes to "1" otherwise, "0" remains.

I also know that I need to reload the section of this particular medicine that day.

Now the question is, how do I achieve this? How to reload a specific section?

 for(NSManagedObject *mo in secondEntityArray) { NSDate *temp1 = [mo valueForKey:@"date"]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSString * temp2 = [dateFormatter stringFromDate:temp1]; NSDate *temp3 = [dateFormatter dateFromString:temp2]; NSDate * temp4 = [dateFormatter dateFromString:_dateLabel.text]; if([temp3 compare:temp4] == NSOrderedSame) { if([[mo valueForKey:@"isNotificationFired"] isEqualToString:@"1"] && [[mo valueForKey:@"repeatDays"] isEqualToString:@"Everyday"]) { //Reflect changes in tableView section } } } 
+9
source share
6 answers

UITableView supports several functions for reloading data. See the section Reloading the table view of a document.

  • reloadData li>
  • reloadRowsAtIndexPaths: withRowAnimation:
  • reloadSections: withRowAnimation:
  • reloadSectionIndexTitles

Also see this similar thread , part of it:

 NSRange range = NSMakeRange(0, 1); NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range]; [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone]; 

In addition, there is no need to reload certain sections, you can simply configure your data set and call [tableView reoadData] , it will load only visible cells. Part from the document:

Call this method to reload all the data that is used to create the table, including cells, section headers, footers, index arrays, and soon. For efficiency, only rows that are visible are displayed in the table view. It corrects offsets if the table is compressed as a result of a reload.

+8
source

Swift 3

NSIndexSet is nothing special

 tableView.reloadSections([1, 2, 3], with: .none) 

It is just an array. Crazy.

+50
source

Use this method to reload a partition.

 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 
+1
source

Here is this method, you can transfer section details in different ways

 [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:NO]; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone]; 

Rebooting individual sections improves the performance for presenting tables, and also avoids some problems, such as floating / moving user headers in your view. SO try to use reloadSection than relaodData when possible

+1
source

Update for quick use in Swift (since version 2.2)

 //where 0 is the section you want to reload tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None) 
+1
source

You can also use this -

indexPath.section - The `UITableView section that you want to reload.

  NSIndexSet *section = [NSIndexSet indexSetWithIndex:indexPath.section]; [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone]; 
+1
source

All Articles