【问题标题】:Hiding UITableView sections with delegate methods not drawing headers correctly afterwards使用委托方法隐藏 UITableView 部分之后不能正确绘制标题
【发布时间】:2014-06-28 22:37:55
【问题描述】:

我的 UITableView 中有两个部分由单个数组的谓词过滤填充作为它们的数据源。当谓词过滤数组中有项目时,标题被正确绘制(着色)并且位于正确的位置。颜色在 willDisplayHeaderView 中设置。

注意,这不是 UITableViewController,而是作为更复杂视图一部分的 UITableView。*

可以使用添加 (+) 按钮将新项目添加到表格中。

当我从一个部分删除项目直到它有 0 个项目时,标题会正确消失。

当我将新项目添加到其中一个空(现在不可见)部分时,标题被正确绘制和命名,但它的颜色不正确并且看起来与它上面的部分略有重叠(或者在第一个的情况下) /0th 部分,它显示在桌面下方,因此您看不到一半的文字)。

现在添加以前不存在的部分时几乎不会调用 willDisplayHeaderView,因为它的数据源现在有数据。

这是我的方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return kSectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    switch (section) {
        case kIncompleteActivitiesSection:
            return [[_activitiesArray filteredArrayUsingPredicate:_predIncomplete] count];
        case kCompleteActivitiesSection:
            return [[_activitiesArray filteredArrayUsingPredicate:_predComplete] count];
        default:
            return 0;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// return the section names
switch (section) {
    case kIncompleteActivitiesSection:
        if ([[_activitiesArray filteredArrayUsingPredicate:_predIncomplete] count] > 0) {
            return @"Upcoming Activities";
        } else {
            return nil;
        }
    case kCompleteActivitiesSection:
        if ([[_activitiesArray filteredArrayUsingPredicate:_predComplete] count] > 0) {
            return @"Completed Activities";
        } else {
            return nil;
        }
    default:
        return 0;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
// set header text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];

// set header background color
header.backgroundView = [UIView new];
header.backgroundView.backgroundColor = _colorSchemeColor;
    header.backgroundView.alpha = 1.0;
}

【问题讨论】:

  • Further... reloadData 会导致标题在正确的位置以正确的颜色重绘,但会导致其余动画(用于添加行)不可见。在测试中,肯定是因为 willDisplayHeaderView 没有触发......有什么办法强制它?

标签: ios uitableview delegates


【解决方案1】:

当您更改 _activitiesArray 时,您必须重新加载受影响的部分。根据我的经验,部分标题不会自动更新。

[self.tableView beginUpdates];
//Update your data model here
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

您可以替换任何您喜欢的行动画样式。

【讨论】:

  • 这很有效。现在标题被绘制在正确的位置,但仍然没有正确着色。 p.s.我不得不注释掉我用来将新对象添加到表中的 insertRowsAtIndexPaths 方法。不过,我认为在这种情况下不会再次调用 willDisplayHeaderView。
  • 如果我这样做:[self.itineraryTableView beginUpdates]; NSIndexSet *sections = [NSIndexSet indexSetWithIndex:kIncompleteActivitiesSection]; [self.itineraryTableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic]; [self.itineraryTableView endUpdates]; UIView *incompleteActivitiesHeader = [self.itineraryTableView headerViewForSection:kIncompleteActivitiesSection]; [self colorizeHeader:incompleteActivitiesHeader]; 并有一个方法 'colorizeHeader' 为标题着色,它最终看起来不错,但会从默认灰色闪烁到我的彩色。
  • ...这不是很好,但有点工作。还有其他想法吗?
  • 您是否在 willDisplayHeaderView 方法中放置了断点或 NSLog() 语句以查看它是否正在触发?此外,最好自己创建标题视图并在 tableView:viewForHeaderInSection: 中提供它。操作系统正在那里寻找标题视图。
  • 我做到了。那就不发火了。奇怪的是它不会只在这种情况下触发。我对其进行了更多调整,并检查这是否是第一个“添加”,如果不是,那么我使用 insertRowsAtIndexPaths 而不是 reloadSections (这不会导致它重绘——它从那里开始保持漂亮的颜色)那里已经有数据了)。现在,当这是第一次添加到表格部分时,我只会得到灰色到彩色的闪烁。哦,好吧,不知道还能做什么。
猜你喜欢
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多