Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload section without reloading section header

Maybe you should try with

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationLeft] //or UITableViewRowAnimationRight

However, I'm not sure but I think it can rise some error in the case where you have less rows to reload than previously.


Edit

I think you could deal with [tableView beginUpdates] and [tableView endUpdates] to solve your problem.

For example, you have 2 arrays of data to display. Let name them oldArray and newArray. A sample of how what you could do :

- (void)selectedSegmentIndexChanged:(UISegmentedControl *)sender
{
    [self.tableView setDataSource: newArray];
    int nbRowToDelete = [oldArray count];
    int nbRowToInsert = [newArray count];

    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < nbRowToInsert; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
    }

    NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < nbRowToDelete; i++) {
        [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
    }

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft];
    [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
    [self.tableView endUpdates];
}

If you are using Swift 2.0, feel free to use this extension.

Be warned: passing in the wrong oldCount or newCount will crash you program.

extension UITableView{

func reloadRowsInSection(section: Int, oldCount:Int, newCount: Int){

    let maxCount = max(oldCount, newCount)
    let minCount = min(oldCount, newCount)

    var changed = [NSIndexPath]()

    for i in minCount..<maxCount {
        let indexPath = NSIndexPath(forRow: i, inSection: section)
        changed.append(indexPath)
    }

    var reload = [NSIndexPath]()
    for i in 0..<minCount{
        let indexPath = NSIndexPath(forRow: i, inSection: section)
        reload.append(indexPath)
    }

    beginUpdates()
    if(newCount > oldCount){
        insertRowsAtIndexPaths(changed, withRowAnimation: .Fade)
    }else if(oldCount > newCount){
        deleteRowsAtIndexPaths(changed, withRowAnimation: .Fade)
    }
    if(newCount > oldCount || newCount == oldCount){
        reloadRowsAtIndexPaths(reload, withRowAnimation: .None)
    }
    endUpdates()

}

Try this:

BOOL needsReloadHeader = YES;
UIView *oldHeaderView = nil;

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerToReturn = nil;
    if(needsReloadHeader == YES) {
        headerToReturn = [[UIView alloc] init];
        // ...
        // custom your header view in this block
        // and save
        // ...
        oldHeaderView = headerToReturn;
    } else {
        headerToReturn = oldHeaderView;
    }
    return headerToReturn;
}

Your just need to change 'needsReloadHeader' to 'NO' in other places.


An objective-c version of Intentss extension

@interface UITableView (Extensions)

- (void)reloadRowsInSection:(NSUInteger)sectionIndex withRowAnimation:(UITableViewRowAnimation)rowAnimation oldCount:(NSUInteger)oldCount newCount:(NSUInteger)newCount;

@end


@implementation UITableView (Extensions)

- (void)reloadRowsInSection:(NSUInteger)sectionIndex withRowAnimation:(UITableViewRowAnimation)rowAnimation oldCount:(NSUInteger)oldCount newCount:(NSUInteger)newCount {

    NSUInteger minCount = MIN(oldCount, newCount);

    NSMutableArray *insert = [NSMutableArray array];
    NSMutableArray *delete = [NSMutableArray array];
    NSMutableArray *reload = [NSMutableArray array];

    for (NSUInteger row = oldCount; row < newCount; row++) {
        [insert addObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
    }

    for (NSUInteger row = newCount; row < oldCount; row++) {
        [delete addObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
    }

    for (NSUInteger row = 0; row < minCount; row++) {
        [reload addObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
    }

    [self beginUpdates];

    [self insertRowsAtIndexPaths:insert withRowAnimation:rowAnimation];
    [self deleteRowsAtIndexPaths:delete withRowAnimation:rowAnimation];
    [self reloadRowsAtIndexPaths:reload withRowAnimation:rowAnimation];

    [self endUpdates];
}

@end