数据源方法实际上与与表视图的属性tableView:heightForHeaderInSection:
关联的视图无关。tableViewHeader
这里有两种不同类型的标题,一种位于 tableView 顶部的标题,可以在其中放置搜索栏之类的东西,而多个标题可以在表格视图中的每个部分出现一个。
据我所知,tableViewHeader
视图通常在 nib 文件中配置,并且我不知道表视图调用任何允许对其进行任何配置的数据源方法,因此您必须手动进行。坦率地说,如果您的代码有效,那将是一个很好的方法。隐藏它会使表格视图仍然像它在那里一样......完全删除它会使它无法取回,因为它被释放了。
(但是,正如您所说,您可以使用指向标题视图的 IBOutlet,只要您将其设置为强引用,然后您可以稍后以某种方式将其重新插入表中。......嗯,虽然如何您将视图添加到表格视图的滚动视图中,并正确定位它,可能只是烦人。)
我唯一的建议是将帧高度设置为零,这样你就可以获得一个很好的过渡效果,比如animateWithDuration。但是,是的,我会说你已经找到了最好的方法。
编辑:
代码,你说?我认为这是一个挑战:)
- (void)setTableViewHeaderHidden:(BOOL)hide
{
// Don't want to muck things up if we are mid an animation.
if (self.isAnimatingHeader) {
return;
}
// This is our IBOutlet property, I am just saving a bit of typing.
UIView *theHeader = self.theHeaderView;
if (hide) {
// Save the original height into the tag, should only be done once.
if (!theHeader.tag) {
theHeader.tag = theHeader.frame.size.height;
}
// Transform and hide
if (theHeader.frame.size.height > 0) {
self.isAnimatingHeader = YES;
// New frame...
CGRect frame = theHeader.frame;
frame.size.height = 0;
// Figure out some offsets here so we prevent jumping...
CGPoint originalOffset = self.tableView.contentOffset;
CGPoint animOffset = originalOffset;
animOffset.y += MAX(0, theHeader.tag - animOffset.y);
CGPoint newOffset = originalOffset;
newOffset.y = MAX(0, newOffset.y - theHeader.tag);
// Perform the animation
[UIView animateWithDuration:0.35
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
theHeader.frame = frame;
self.tableView.contentOffset = animOffset;
}
completion:^(BOOL finished){
if (finished) {
// Hide the header
self.tableView.tableHeaderView = nil;
theHeader.hidden = YES;
// Shift the content offset so we don't get a jump
self.tableView.contentOffset = newOffset;
// Done animating.
self.isAnimatingHeader = NO;
}
}
];
}
} else {
// Show and transform
if (theHeader.frame.size.height < theHeader.tag) {
self.isAnimatingHeader = YES;
// Set the frame to the original before we transform, so that the tableview corrects the cell positions when we re-add it.
CGRect originalFrame = theHeader.frame;
originalFrame.size.height = theHeader.tag;
theHeader.frame = originalFrame;
// Show before we transform so that you can see it happen
self.tableView.tableHeaderView = theHeader;
theHeader.hidden = NO;
// Figure out some offsets so we don't get the table jumping...
CGPoint originalOffset = self.tableView.contentOffset;
CGPoint startOffset = originalOffset;
startOffset.y += theHeader.tag;
self.tableView.contentOffset = startOffset; // Correct for the view insertion right off the bat
// Now, I don't know if you want the top header to animate in or not. If you think about it, you only *need* to animate the header *out* because the user might be looking at it. I figure only animate it in if the user is already scrolled to the top, but hey, this is open to customization and personal preference.
if (self.animateInTopHeader && originalOffset.y == 0) {
CGPoint animOffset = originalOffset;
// Perform the animation
[UIView animateWithDuration:0.35
delay:0.0
options: UIViewAnimationCurveEaseIn
animations:^{
self.tableView.contentOffset = animOffset;
}
completion:^(BOOL finished){
// Done animating.
self.isAnimatingHeader = NO;
}
];
} else {
self.isAnimatingHeader = NO;
}
}
}
}
在 Xcode 附带的表格视图模板中构建了它。只是为了把它放在一起,我使用了一个UILongPressGestureRecognizer
指向这个方法的选择器出口:
- (IBAction)longPress:(UIGestureRecognizer *)sender
{
if (sender.state != UIGestureRecognizerStateBegan) {
return;
}
if (self.hidingHeader) {
self.hidingHeader = NO;
[self setTableViewHeaderHidden:NO];
} else {
self.hidingHeader = YES;
[self setTableViewHeaderHidden:YES];
}
}
而且,我将这些添加到我的标题中:
@property (strong, nonatomic) IBOutlet UIView *theHeaderView;
@property (nonatomic) BOOL hidingHeader;
@property (nonatomic) BOOL isAnimatingHeader;
@property (nonatomic) BOOL animateInTopHeader;
- (IBAction)longPress:(id)sender;
无论如何,它工作得很好。我确实发现,您肯定必须取消表格视图对标题视图的引用,否则它不会消失,并且表格视图将根据标题框架的高度移动单元格的位置被分配回其标题属性。此外,您必须通过 IBOutlet 保持对标题的强引用,否则当您取消表视图对标题的引用时,它会被丢弃。
干杯。