我已实现此方法以返回节标题高度。但是,当部分的高度发生变化时,会立即发生,而不会生成动画。
是否有动画?
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (flatHeader) {
return 50.0f;
} else {
return 100.0f;
}
}
答案 0 :(得分:24)
我不是100%确定这适用于表格标题,但它适用于表格行,因此值得一试。我有一个实例变量headerHeight
,最初设置为44.0,我将其更改为:
- (void)changeHeight {
[self.tableView beginUpdates];
headerHeight = 88.0;
[self.tableView endUpdates];
}
在我的代码中,我返回headerHeight
中的heightForRowAtIndexPath
,但您可以在heightForHeaderInSection
中尝试:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return headerHeight;
}
答案 1 :(得分:12)
这有效:
flatHeader = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[[self tableView] beginUpdates];
[[self tableView] endUpdates];
CGRect frame = [[self headerView] frame];
frame.size.height = [self tableView:[self tableView] heightForHeaderInSection:0];
[[self headerView] setFrame:frame];
[UIView commitAnimations];
答案 2 :(得分:2)
我在Swift中的解决方案:
class MyTableViewController: UITableViewController {
var sectionHeaderView:UIView?
...
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
sectionHeaderView = UIView(frame: CGRectMake(0, 0, self.tableView.frame.size.width, 30))
sectionHeaderView?.backgroundColor = UIColor.grayColor()
var button = UIButton(frame: CGRectMake(0, 0, self.tableView.frame.size.width, 30))
button.backgroundColor = UIColor.darkGrayColor()
button.setTitle("collapse/expand", forState: .Normal)
button.addTarget(self, action: "collapseOrExpandSectionHeader", forControlEvents: .TouchUpInside)
sectionHeaderView?.addSubview(button)
return sectionHeaderView
}
...
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if let sectionHeader = sectionHeaderView {
return view.frame.size.height
} else {
return 30.0
}
}
...
func collapseOrExpandSectionHeader() {
if let sectionHeader = sectionHeaderView {
let headerHeight:CGFloat
if sectionHeader.frame.size.height == 200 {
headerHeight = 30.0
} else {
headerHeight = 200.0
}
UIView.animateWithDuration(0.3, animations: {
self.tableView?.beginUpdates()
sectionHeader.frame.size.height = headerHeight
self.tableView?.endUpdates()
} )
}
}
答案 3 :(得分:-1)
我没有对此进行过测试,但有时当我的UITableViewCells改变高度时,我会收到不需要的动画。原因是我画了自己的细胞,我使用CALayers这样做。在单元格(void)layoutSubviews
中,我会将CALayer的大小更改为单元格框架的大小
myLayer.frame = self.bounds;
当CALayer的frame / bounds属性发生变化时,它会被动画化。所以在理论上,我会说你可以使用方法tableView:viewForHeaderInSection:这将允许你绘制自己的节头。您可以返回一个实现(void)layoutSubviews
的UIView然后在该方法中执行
self.layer.frame = self.bounds;
只是一个想法。