在UITableView中重新加载部分

问题描述 投票:9回答:6

我正在编写一个应用程序,通知用户何时服用他/她的药物。页面顶部的标签显示日期,tableView将填充医学名称和特定日期需要的时间。现在根据当天要服用的药物数量来填充这些部分。因此,部分的数量随着时间的推移而动态变化;计划在该特定日期服用一种药物。每当用户服用药物时,它作为变量存储在DB中,变为"1",否则它仍然是"0"

另外我知道我需要在那天重新加载特定药物的一部分。

现在问题是我如何实现这一目标?如何重新加载特定部分?

for(NSManagedObject *mo in secondEntityArray) {
    NSDate *temp1 = [mo valueForKey:@"date"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString * temp2 = [dateFormatter stringFromDate:temp1];
    NSDate *temp3 = [dateFormatter dateFromString:temp2];
    NSDate * temp4 = [dateFormatter dateFromString:_dateLabel.text];
    if([temp3 compare:temp4] == NSOrderedSame) {
        if([[mo valueForKey:@"isNotificationFired"] isEqualToString:@"1"] && [[mo valueForKey:@"repeatDays"] isEqualToString:@"Everyday"]) {
            //Reflect changes in tableView section
        }
    }
}
ios objective-c uitableview
6个回答
8
投票

UITableView支持重新加载数据的功能很少。参见doc的Reloading the Table View部分。

  • reloadData
  • reloadRowsAtIndexPaths:withRowAnimation:
  • reloadSections:withRowAnimation:
  • reloadSectionIndexTitles

另外,请参阅this similar thread,它的一部分:

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

此外,没有必要重新加载特定的部分,您可以只调整您的数据集并调用[tableView reoadData],它将只加载可见的单元格。 doc的一部分:

调用此方法可重新加载用于构造表的所有数据,包括单元格,节页眉和页脚,索引数组等。为了提高效率,表视图仅重新显示可见的行。如果表因重新加载而缩小,它会调整偏移量。


53
投票

斯威夫特3

NSIndexSet没什么特别的

tableView.reloadSections([1, 2, 3], with: .none)

这只是一个阵列。疯。


1
投票

使用此方法重新加载您的部分

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

1
投票

这是方法,您可以通过不同方式传递部分详细信息

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

重新加载特定部分可以提高表视图的性能,同时还可以避免一些问题,例如在视图中浮动/移动自定义页眉。所以尽可能尝试使用reloadSection而不是relaodData


1
投票

更新以便在Swift中快速使用(从2.2开始)

//where 0 is the section you want to reload
tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)

1
投票

你也可以用它 -

indexPath.section - 您要重新加载的`UITableView部分。

 NSIndexSet *section = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
© www.soinside.com 2019 - 2024. All rights reserved.