【问题标题】:how to reload tableview's particular section如何重新加载 tableview 的特定部分
【发布时间】:2021-07-27 16:23:16
【问题描述】:

我的表格视图中有 56 个部分,我将当前选择的部分编号存储在一个名为“activeTimeSlot”的整数变量中。如何使用以下方法重新加载当前选择的部分。我正在这样做

 [self.tblView reloadSections:[NSIndexSet indexSetWithIndex:activeTimeSlot] withRowAnimation:UITableViewRowAnimationAutomatic];

但我观察到这样做是为所有部分调用 tableview 的以下数据源方法,即重新加载所有部分。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

我不确定如何使用 NSIndexSet 以及如何使用 NSIndexSet 重新加载超过 1 个部分。

【问题讨论】:

  • 查看我的回答,希望对您有所帮助。
  • 您是如何发现正在重新加载所有部分的?
  • 因为它对所有部分的调用 numberOfRowsInSection 方法@NicolaMiotto 我的意思是 56 次

标签: ios objective-c uitableview


【解决方案1】:

如果你想更新多个部分,那么你可以这样做。

NSMutableIndexSet *indetsetToUpdate = [[NSMutableIndexSet alloc]init];

[indetsetToUpdate addIndex:previousSection]; // [indetsetToUpdate addIndex:<#(NSUInteger)#>] 
// You can add multiple indexes(sections) here.

[detailTableView reloadSections:indetsetToUpdate withRowAnimation:UITableViewRowAnimationFade];

这用于更新多个部分,对我来说效果很好。

【讨论】:

  • 好的,但是为什么重新加载一个部分会询问数据源所有部分的行数?像 [self.tblView reloadSections:[NSIndexSet indexSetWithIndex:10] withRowAnimation:UITableViewRowAnimationAutomatic];在 Section 方法中调用 numberOFRows 56 次。
  • 您的部分中可能有这么多行。
  • 如果我只重新加载一个部分并为所有部分调用 numberOfRowsInSection 方法是否正常?
  • 它应该调用的特定部分。如果您要更改其他任何内容,那么它将仅针对特定部分调用。
  • 不,它会为所有部分调用 numberOfRowsInSection,检查接受的答案。你的回答也有帮助,我问题的第二部分:)
【解决方案2】:

正如我从您的 cmets 看到的,您正在检查方法的调用

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

找出它正在重新加载每个部分。但是可能出于内部原因,所有部分都调用了该方法。如果您尝试记录实际重新加载的行,您会发现一切都很好。放一个

NSLog(@"Section %d", indexPath.section);

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

并看到仅重新加载所选部分中的行。

PS:忘了说:你的解决方案已经正确了。

【讨论】:

  • 是的,它仅针对该特定部分调用 cellForRowAtIndexPath ..我太懒了,没有检查我假设如果它对所有部分调用 numberOfRows,那么肯定它必须为所有部分调用 cellForRowAtIndexPath .. :) 无论如何谢谢..)
猜你喜欢
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2021-05-12
  • 2013-07-30
  • 2017-11-18
  • 2016-11-09
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多