【问题标题】:Reload sections in UITableView在 UITableView 中重新加载部分
【发布时间】:2015-09-02 11:35:35
【问题描述】:

我正在编写一个应用程序,通知用户何时该服药。页面顶部的标签显示了一个日期,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


    【解决方案1】:

    斯威夫特 3

    NSIndexSet 没什么特别的

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

    这只是一个数组。疯了。

    【讨论】:

    • 这是一个很好的解决方案
    • 你必须先定义函数......然后使用它。 func reloadSections(_sections: IndexSet, with animation: UITableViewRowAnimation)
    • 非常简单的解决方案
    • @Martian2049 如果您使用 UITableViewController,它将是一个现有属性,否则您必须通过界面构建​​器为您的 tableview 属性创建一个出口,或者从头开始实例化一个并给它一个框架等...
    【解决方案2】:

    UITableView 支持的函数很少用于重新加载数据。请参阅文档的Reloading the Table View 部分。

    • 重新加载数据
    • reloadRowsAtIndexPaths:withRowAnimation:
    • reloadSections:withRowAnimation:
    • 重新加载SectionIndexTitles

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

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

    另外,不需要重新加载特定部分,您只需调整数据集并调用[tableView reloadData],它只会加载可见单元格。文档的一部分:

    调用此方法重新加载所有用于构建的数据 表,包括单元格、节页眉和页脚、索引数组和 很快。为了提高效率,表格视图只重新显示那些 可见。如果表格由于以下原因而缩小,它会调整偏移量 重新加载。

    【讨论】:

    • 实际上,我最初是用核心数据中第一个实体的数据加载表格视图,后来我试图从第二个实体中的数据重新加载它。这是我第一次做这一切我是初学者,所以我真的搞砸了,现在我意识到了。我已经知道你提到的所有方法。但无论如何感谢您的帮助。
    【解决方案3】:

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

    // index 0 being the section number 1 that you will reload
    tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 老兄,我知道要使用的方法,但我不明白如何使用 NSIndexSet 来跟踪我的部分的索引
      • 像这样创建索引 [NSIndexSet indexSetWithIndex:yourIndexOfSectionToReload]
      【解决方案5】:

      这里是方法,你可以通过不同的方式传递部分细节

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

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

      【讨论】:

        【解决方案6】:

        你也可以用这个--

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

         NSIndexSet *section = [NSIndexSet indexSetWithIndex:indexPath.section];
        [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-01
          相关资源
          最近更新 更多