【发布时间】: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