简体   繁体   English

如何在UITableView中为节标题的高度变化设置动画?

[英]How to animate the height change of an section header in UITableView?

I've implemented this method to return the section header height. 我已经实现了这个方法来返回节头高度。 However, when the height for the section changes, it happens immediately without animation. 但是,当部分的高度发生变化时,会立即发生,而不会生成动画。

Is there an animation for that? 那有动画吗?

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (flatHeader) {
        return 50.0f;
    } else {
        return 100.0f;
    }
}

I'm not 100% sure this will work for a table header but it works for table rows so it's worth a shot. 我不是100%肯定这将适用于表头,但它适用于表行,所以它值得一试。 I have an instance variable headerHeight initially set to 44.0 and I change it as so: 我有一个实例变量headerHeight初始设置为44.0,我改为:

- (void)changeHeight {
    [self.tableView beginUpdates];
    headerHeight = 88.0;
    [self.tableView endUpdates];
}

In my code I return the headerHeight in heightForRowAtIndexPath but you can try it in heightForHeaderInSection : 在我的代码中,我在heightForRowAtIndexPath返回headerHeight ,但您可以在heightForHeaderInSection尝试它:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return headerHeight;
}

This works: 这有效:

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];

My solution in Swift: 我在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()
        } )
    }
}

I haven't tested this, but sometimes I get unwanted animations when my UITableViewCells change height. 我没有对此进行测试,但有时当我的UITableViewCells改变高度时,我会得到不需要的动画。 The cause of this is that I draw my own cells, and I use CALayers to do so. 原因是我画了自己的细胞,我使用CALayers这样做。 In the cell's (void)layoutSubviews I would change the size of my CALayer to be the size of the frame for the cell 在单元格(void)layoutSubviews我会将CALayer的大小更改为单元格框架的大小

myLayer.frame = self.bounds;

When the frame/bounds property of a CALayer changes, it is animated. 当CALayer的frame / bounds属性发生变化时,它会被动画化。 So in theory, I would say that you could use the method tableView:viewForHeaderInSection: which would allow you to draw your own section header. 所以在理论上,我会说你可以使用方法tableView:viewForHeaderInSection:这将允许你绘制自己的节头。 You could just return a UIView that implements (void)layoutSubviews and then in that method do 您可以返回一个实现(void)layoutSubviews的UIView,然后在该方法中执行

self.layer.frame = self.bounds;

Just an idea. 只是一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM