web-dev-qa-db-fra.com

Section de rechargement UITableView

Je veux recharger une seule section, pas le tableau complet. Existe-t-il une méthode dans UITableView.

[tableView reloadData] est utilisé pour charger la table complète.
Je veux savoir comment charger une seule section, car j'ai un grand nombre de lignes dans la table.

39
Anil Kothari

Oui il y a:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
33
sosborn

La méthode reloadSections me perturbe, car je dois construire quelques objets. C'est formidable si vous avez besoin de flexibilité, mais parfois, je veux aussi simplement la simplicité. Ça va comme ça:

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

Cela rechargera la première section. Je préfère avoir une catégorie sur UITableView et simplement appeler cette méthode:

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

Ma méthode de catégorie ressemble à ceci:

@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];
}
69
bandejapaisa

Mais vous ne pouvez recharger que les sections qui contiennent le même nombre de lignes/ (ou vous devez les ajouter ou les supprimer manuellement). Sinon, vous obtiendrez:

*** 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).'

Ce qui n'est pas nécessaire lorsque vous utilisez [tableView reloadData].

Lorsque vous avez besoin de recharger une section et que vous avez changé le nombre de lignes qu'elle contient, vous pouvez utiliser quelque chose comme ceci:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

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

Si vous le mettez dans une catégorie (comme les spectacles de bandejapaisa), cela pourrait ressembler à ceci:

- (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];
}
22
Inza

Pour Swift 3 et Swift 4

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

self.tableView.reloadSections(indexSet, with: .automatic)
10
pableiros

que la bonne façon:

[self.tableView beginUpdates]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
6
Ofir Malachi

Sur la base de la réponse acceptée ici, j'ai créé une fonction qui recharge toutes les sections du tableau à l'aide d'une animation. Cela pourrait probablement être optimisé en ne rechargeant que les sections visibles.

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

Dans mon cas, j'ai dû forcer un reloadData avant l'animation de la section, car les données sous-jacentes de la table avaient été modifiées. Il s'anime correctement cependant.

4
Nick

Voici la méthode, vous pouvez passer les détails de la section de différentes manières

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

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

Le rechargement de sections particulières améliore les performances de la vue table et évite également certains problèmes tels que le flottement/le déplacement des en-têtes/pieds de page personnalisés dans votre vue. SO essayez d'utiliser reloadSection plutôt que relaodData chaque fois que possible

2
Aks

Vous avez besoin de ça ... Pour Recharger la Ligne

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

ou pour la section Recharger

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
2
Ankit Srivastava

Essayez d'utiliser

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

J'espère que cela résoudra votre problème.

2
Vineesh TP

Si vous avez une vue en coupe personnalisée, vous pouvez y ajouter une référence faible dans votre contrôleur de vue et la mettre à jour à tout moment. Voici mon code pour référence:

@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;
}

Ensuite, vous pourrez mettre à jour votre section comme suit:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableHeaderLabel.text = [NSString stringWithFormat:@"Showing row: %ld", indexPath.row];
}
0
Kamen Dobrev