我希望重新加载一个部分而不是整个表格。在UITableView
中有没有相应的方法。
[tableView reloadData]
用于加载整个表格。
我想知道如何只加载一个部分,因为我的表格中有大量的行。
我希望重新加载一个部分而不是整个表格。在UITableView
中有没有相应的方法。
[tableView reloadData]
用于加载整个表格。
我想知道如何只加载一个部分,因为我的表格中有大量的行。
reloadSections方法让我感到困扰--因为我必须构建一些对象。如果需要灵活性,这很棒,但有时也只是想要简单性。它的用法如下:
NSRange range = NSMakeRange(0, 1);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
这将重新加载第一部分。我更喜欢在UITableView上有一个类别,只需调用此方法:
[self.tableView reloadSectionDU:0 withRowAnimation:UITableViewRowAnimationNone];
我的分类方法看起来像这样:
@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];
}
[NSIndexSet indexSetWithIndex:1]
来替代range
。 - Islam是的,有这种情况:
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
但是你只能重新加载那些包含相同数量行的节(否则你需要手动添加或删除它们)。否则,你将会遇到以下错误:
*** 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).'
这种情况下,你可以使用[tableView reloadData]
,就不需要上述操作。
当你需要重新加载一个节,并且节内行数已更改时,你可以使用如下方法:
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
[self beginUpdates];
[self deleteSections:indexSet withRowAnimation:rowAnimation];
[self insertSections:indexSet withRowAnimation:rowAnimation];
[self endUpdates];
如果你把它放在一个类别下面(比如像bandejapaisa展示那样),它可能看起来是这样的:
- (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];
}
适用于 Swift 3、4 和 5
let sectionToReload = 1
let indexSet: IndexSet = [sectionToReload]
self.tableView.reloadSections(indexSet, with: .automatic)
正确的方式是:
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
根据这里接受的答案,我制作了一个函数,可以使用动画重新加载表格中的所有部分。这可能通过仅重新加载可见部分进行优化。
[self.tableView reloadData];
NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];
您需要这个... 用于重新加载行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
[self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:NO];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
重新加载特定的部分可以提高表格视图的性能,有时还可以避免一些问题,比如在视图中浮动/移动自定义标题-页脚。因此,请尽可能使用reloadSection而不是reloadData。
尝试使用
[self.tableView beginUpdates];
[self.tableView endUpdates];
但是你只能重新加载包含相同行数的部分(或者你必须手动添加或删除它们)。否则,你将会收到一个NSInternalInconsistencyException。
步骤:
现在你可以安全地调用reloadSections :) 重新加载部分将调用其余索引的更新。
或者,你可以使用像这样的库:https://github.com/onmyway133/DeepDiff
Swift伪代码:
tableView.deleteRows(at: valueIndexesToRemove, with: .automatic)
tableView.insertRows(at: valueIndexesToInsert, with: .automatic)
tableView.reloadSections(IndexSet([section]), with: .automatic)