Reckoner Reckoner - 2 months ago 9x
Objective-C Question

Reload sections in UITableView

It goes like this :
I am writing an app that notifies user when its time to take his/her medicine.The label at the top of the page shows a date and the tableView gets populated with medicine names and times that need to be taken on that particular day.Now the sections get populated on the basis of number of medicines to be taken on that day.So the number of sections are dynamically changing overtime a medicine is scheduled to be taken on that particular day.Whenever the user takes a medicine,it is stored in DB as a variable that changes to "1" else it remains "0".
Also I know that I need to reload section of that particular medicine on that day.
Now the question is how do I achieve that?How to reload a particular 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



There are few functions supported in UITableView for reloading data. See section Reloading the Table View of doc.

  • reloadData
  • reloadRowsAtIndexPaths:withRowAnimation:
  • reloadSections:withRowAnimation:
  • reloadSectionIndexTitles

Also, see this similar thread, a part from it:

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

Also, it is not necessary to reload specific sections, you can just adjust your dataset and call [tableView reoadData], it will load the visible cells only. A part from doc:

Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload.