【问题标题】:Change UITableView section header/footer WHILE RUNNING the app?在运行应用程序时更改 UITableView 部分页眉/页脚?
【发布时间】:2010-12-05 13:44:03
【问题描述】:

我遇到了一个我无法解决的问题... 我有一个分组表,其部分页眉和部分页脚在启动时正确显示,这要归功于

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

然而,我不知道以后如何更新它们,我不知道如何更改这些文本,这很令人沮丧,因为在我看来,这比更改文本标签或其他任何事情都要难。 .(一些“.text”属性...)

我搜索了所有文档都没有运气......

非常感谢任何帮助! 问候, 恩里科

【问题讨论】:

  • 有没有办法更新页眉/页脚而不重新加载整个表格...或整个部分?更新页眉/页脚。
  • 我不相信只更新 tableview 的一部分会给你带来任何好处,或者你会看到性能上的差异。

标签: iphone uitableview header title footer


【解决方案1】:

在委托方法中,添加一个返回节名的方法调用,例如:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case firstSectionTag:
            return [self firstSectionTitle];
        case secondSectionTag:
            return [self secondSectionTitle];
        // ...
        default:
            return nil;
    }
}

- (NSString *)firstSectionTitle {
    // generate first section title programmatically, e.g. "return [[NSDate date] description];" 
}

// ...

然后,当您需要更新部分标题时,发送NSNotification 触发类似以下方法的内容:

- (void)refreshTableSectionTitles:(NSNotification *)notification {
    [tableView reloadData];
}

如果表很大并且您想要更好的控制,请传递一个 NSNotification 和一个包含您要重新加载的部分的 NSDictionary,阅读 -refreshTableSectionTitles 中的字典以取回 NSInteger 部分,然后使用表格视图的-reloadSections:withRowAnimation: 重新加载该特定部分。

【讨论】:

    【解决方案2】:

    你可以使用:

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

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    

    在语句中定义新的标题文本。

    这将在更改页眉/页脚时制作漂亮的动画。

    【讨论】:

      【解决方案3】:

      在您要更改标题的代码中使用此行:

          [[self theTableViewToChangeItsHeader]reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
      

      【讨论】:

      • 在 iOS 7 上,这会导致该部分中的单元格消失。
      • 这个问题与ios 5.1有关。您是否认真地否决了它? :D
      【解决方案4】:

      在你的 TableViewController 中使用

      [self.tableView reloadSectionIndexTitles];
      

      【讨论】:

      • 不重新加载页脚文本
      • 这仅适用于滚动时沿屏幕右侧显示的索引栏。
      猜你喜欢
      • 2011-02-12
      • 2010-12-20
      • 1970-01-01
      • 2019-09-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多