UITableView reload section
Asked Answered
E

11

49

I want to reload only one section not the full table. Is there any method in UITableView.

[tableView reloadData] is used to load full table.
I want to know how to load only one section, as I have large number of rows in the table.

Evelineevelinn answered 29/11, 2011 at 6:29 Comment(2)
Calling reloadData still only will initially reload the UITableViewCells that are on screenPuppy
possible duplicate of #3485662Force
M
37

Yes, there is:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
Mufinella answered 29/11, 2011 at 6:30 Comment(0)
R
71

The reloadSections method bugs me -- as I have to construct a few objects. This is great if you need the flexibility, but sometimes I also just want the simplicity too. It goes like this:

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

This will reload the first section. I prefer to have a category on UITableView and just call this method:

[self.tableView reloadSectionDU:0 withRowAnimation:UITableViewRowAnimationNone];

My category method looks like this:

@implementation UITableView (DUExtensions)

- (void) reloadSectionDU:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation {
    NSRange range = NSMakeRange(section, 1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];                                     
    [self reloadSections:sectionToReload withRowAnimation:rowAnimation];
}
Rejuvenate answered 29/5, 2012 at 17:25 Comment(3)
Love the @implementation function! Thanks for making it so simplePercy
Isn't easier to invoke [NSIndexSet indexSetWithIndex:section] ?Looksee
Or you could do [NSIndexSet indexSetWithIndex:1] (instead of range)Shantishantung
M
37

Yes, there is:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
Mufinella answered 29/11, 2011 at 6:30 Comment(0)
O
27

But you can reload only sections which contain same number of rows (or you have to manually add or remove them). Otherwise you will get:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Which is not required when you use [tableView reloadData].

When you need to reload a section and you have changed number of rows inside it, you could use something like this:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

[self beginUpdates];
    [self deleteSections:indexSet withRowAnimation:rowAnimation];
    [self insertSections:indexSet withRowAnimation:rowAnimation];
[self endUpdates];

If you put it in a category (like bandejapaisa shows) it could look like this:

- (void)reloadSection:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation {
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

    [self beginUpdates];
        [self deleteSections:indexSet withRowAnimation:rowAnimation];
        [self insertSections:indexSet withRowAnimation:rowAnimation];
    [self endUpdates];
}
Ogletree answered 22/10, 2012 at 11:32 Comment(3)
And in which case? Now better approach might be to use the KVO (and thus receive the posibility of automatic UITableView reloading based on changes to some model) - or if you use CoreData you might be interested in NSFetchResultsController.Ogletree
I got into this problem. I just reload the problematic section before reloading the actual section. It solved the problem.Prisilla
My man, this helped me a lot!Uptrend
C
21

For Swift 3, 4 and 5

let sectionToReload = 1
let indexSet: IndexSet = [sectionToReload]

self.tableView.reloadSections(indexSet, with: .automatic)
Crabber answered 17/2, 2017 at 16:0 Comment(0)
F
6

that the correct way:

[self.tableView beginUpdates]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Frustration answered 4/5, 2016 at 11:6 Comment(0)
M
4

Based on the accepted answer here, I made a function that will reload all sections in the table using an animation. This could probably be optimized by reloading only visible sections.

[self.tableView reloadData];
NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];

In my case, I had to force a reloadData before the section animation, because the underlying data for the table had changed. It animates properly however.

Maraud answered 10/4, 2013 at 15:14 Comment(0)
F
2

You need this... For Reload Row

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

or For Reload section

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
Force answered 29/11, 2011 at 6:31 Comment(0)
G
2

Try to use

[self.tableView beginUpdates]; 
[self.tableView endUpdates];

Hope this will solve your issue.

Guanajuato answered 14/8, 2013 at 8:51 Comment(0)
B
2

Here is the method, you can pass section details in different ways

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

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

Reloading particular sections improves performance for the table view as well some time it also avoid some issues like floating/moving custom headers-footers in your view. SO try to use reloadSection than relaodData whenever possible

Buffet answered 23/8, 2016 at 6:13 Comment(0)
C
1

But you can reload only sections which contain same number of rows (or you have to manually add or remove them). Otherwise you will get an NSInternalInconsistencyException.

Steps:

  1. calculate which rows to remove and/or insert
  2. generate an IndexPath array from these
  3. call related tableView methods

now you can safely call reloadSections :) Reload section will call update for the rest of the indexes.

Or you can use a library like : https://github.com/onmyway133/DeepDiff

Swift pseodo code:


        tableView.deleteRows(at: valueIndexesToRemove, with: .automatic)
        tableView.insertRows(at: valueIndexesToInsert, with: .automatic)
        tableView.reloadSections(IndexSet([section]), with: .automatic)

Corked answered 25/3, 2020 at 19:4 Comment(0)
D
0

If you have custom section view you can add a weak reference to it in your view controller and update it whenever you want. Here is my code for reference:

@property (weak, nonatomic) UILabel *tableHeaderLabel;

....

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *myHeader = [[UITableViewHeaderFooterView alloc] init];

    UILabel *titleLabel = [[UILabel alloc] init];
    [titleLabel setFrame:CGRectMake(20, 0, 280, 20)];
    [titleLabel setTextAlignment:NSTextAlignmentRight];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setFont:[UIFont systemFontOfSize:12]];

    [myHeader addSubview:titleLabel];

    self.tableHeaderLabel = titleLabel; //save reference so we can update the header later

    return myHeader;
}

Then later on you can update your section like this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableHeaderLabel.text = [NSString stringWithFormat:@"Showing row: %ld", indexPath.row];
}
Denney answered 2/10, 2014 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.